'Absolute path in dotnet watch run command doesn't work

To run dotnet core application with specified absolute path we need to run following command:

dotnet run -p C:\foo\bar\Project\Project.csproj

But it seems it doesn't work the same with dotnet watch run:

watch : Could not find a MSBuild project file in 'C:\directory\where\we\execute\command'. Specify which project to use with the --project option.

Running the same command with -project instead of -p doesn't help however...

Dotnet watch help specifies -p or -project parameter anyway:

Microsoft DotNet File Watcher 2.1.1-rtm-30846

Usage: dotnet watch [options] [[--] ...]

Options: -?|-h|--help Show help information
-p|--project The project to watch -q|--quiet Suppresses all output except warnings and errors -v|--verbose
Show verbose output --list Lists all discovered files without starting the watcher --version Show version information

Environment variables:

DOTNET_USE_POLLING_FILE_WATCHER When set to '1' or 'true', dotnet-watch will poll the file system for changes. This is required for some file systems, such as network shares, Docker mounted volumes, and other virtual file systems.

DOTNET_WATCH dotnet-watch sets this variable to '1' on all child processes launched.

Remarks: The special option '--' is used to delimit the end of the options and the beginning of arguments that will be passed to the child dotnet process. Its use is optional. When the special option '--' is not used, dotnet-watch will use the first unrecognized argument as the beginning of all arguments passed into the child dotnet process.

For example: dotnet watch -- --verbose run

Even though '--verbose' is an option dotnet-watch supports, the use of '--' indicates that '--verbose' should be treated instead as an argument for dotnet-run.

Examples: dotnet watch run dotnet watch test

What's wrong then? Why absolute path to project doesn't work with dotnet watch run while works with dotnet run?



Solution 1:[1]

You can resolve this by specifying the -p (or the longer --project) option on the watch command rather than on the run command. In your case, that would be:

dotnet watch -p C:\foo\bar\Project\Project.csproj run

There's a note in the docs that covers this:

You can use dotnet watch --project <PROJECT> to specify a project to watch. For example, running dotnet watch --project WebApp run from the root of the sample app will also run and watch the WebApp project.

Solution 2:[2]

I'm not 100% sure, but dotnet watch is looking for file changes in the current directory. So if you use absolute path it must know where should it looks for changes. Of course, such implementation is possible but I just think that nobody thinked about it when implementing watch command

Solution 3:[3]

In my case, its just a minor error, you have to enter in the project directory before executing dotnet command, like:

cd yourAppName dotnet watch run It'll run

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dbc
Solution 2 Piotr Stapp
Solution 3 Ahsan Anwar