In one of my earlier posts, I had written about creating build and test tasks in VS Code for dotnet core app. The example used in that post had the project.json at the root folder of the app itself, so if we create tasks.json using VS Code’s default options, hitting CTRL + SHIFT + B will build the project. But, if we consider the example I had used in my previous post, where the app comprised two different projects – a console app and a class library project then, the default tasks.json created by VS Code wouldn’t work as such.

The tasks.json would look like the one below :


{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [],
"isBuildCommand": true,
"showOutput": "silent",
"problemMatcher": "$msCompile"
}
]
}

Presssing CTRL + SHIFT + B  would result in the following output:


Couldn't find 'project.json' in current directory

The reason for this error is because, at the root folder, there are no Project.json file available which the dotnet cli can use to build the project. To fix this, we need to pass the appropriate project name (i.e, the folder that contains the corresponding Project.json file) as arguments to the build command. The modified tasks.json would trigger the build successfully.


{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": ["demoLibrary","app"],
"isBuildCommand": true,
"showOutput": "silent",
"problemMatcher": "$msCompile"
}
]
}

Notice the addition of the args demoLibrary and app . The projects will build in the same order as they are specified in the arguments.


Project demoLibrary (.NETStandard,Version=v1.6) was previously compiled. Skipping compilation.
Project app (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.

The other options that dotnet cli build command takes are as follows:


Usage: dotnet build [arguments] [options]

Arguments:
<PROJECT> The project to compile, defaults to the current directory. Can be one or multiple paths to project.json, project directory or
globbing pattern that matches project.json files

Options:
-h|--help Show help information
-o|--output <OUTPUT_DIR> Directory in which to place outputs
-b|--build-base-path <OUTPUT_DIR> Directory in which to place temporary outputs
-f|--framework <FRAMEWORK> Compile a specific framework
-r|--runtime <RUNTIME_IDENTIFIER> Produce runtime-specific assets for the specified runtime
-c|--configuration <CONFIGURATION> Configuration under which to build
--version-suffix <VERSION_SUFFIX> Defines what `*` should be replaced with in version field in project.json
--build-profile Set this flag to print the incremental safety checks that prevent incremental compilation
--no-incremental Set this flag to turn off incremental build
--no-dependencies Set this flag to ignore project to project references and only build the root project

Depending on the need, we can pass any of these options as arguments to the task’s commands and execute them directly from VS Code.

Advertisements