dotnet / Scaffolding

Code generators to speed up development.
MIT License
643 stars 230 forks source link

Code generator fails to run with custom bin and obj location #776

Open hin-hin opened 6 years ago

hin-hin commented 6 years ago

I followed the instructions at https://www.natemcmaster.com/blog/2017/11/13/dotnet-watch-and-docker/ to setup docker with dotnet watch, which requires moving bin/ and obj/ out of the project folder using a Directory.Build.props file so that docker and Visual Studio Code can work together.

Then when I tried to run dotnet aspnet-codegenerator it failed with the following error (also an obj/ folder is created in the project):

Scaffolding failed. Failed to get Project Context for .

To see more information, enable tracing by setting environment variable 'codegen_trace' = 1. RunTime 00:00:00.95

If I remove the Directory.Build.props file then the code generator runs without error.

It seems that the tool does not read the settings inside Directory.Build.props and does not provide a way to specify the path to the obj/ folder (e.g. dotnet ef supports --msbuildprojectextensionspath).

Is there any other way to get the code generator to run other than removing Directory.Build.props?

iAlexanderMoon commented 4 years ago

Thank you for posting this issue.

Confirmed still an issue and temporarily removing Directory.Build.props works.

flowhi commented 3 years ago

Hi,

I can confim this issue with .NET SDK version 5.0.103 and dotnet-aspnet-codegenerator version 5.0.2. If you use build customization which will re-direct your binaries in a different directory, the dotnet-aspnet-codegenerator will produce the following error.

Error:

PS D:\git\project\src\SomeCsproj> dotnet aspnet-codegenerator -h

...
Scaffolding failed.
Failed to get Project Context for D:\git\project\src\SomeCsproj\SomeCsproj.csproj
...

Here is how you can reproduce:

Create file PS D:\git\project\Directory.Build.props

Content of this file must be:

PS D:\git\project> cat .\Directory.Build.props
<Project>
    <PropertyGroup>
        <_OutputPath>$(MSBuildThisFileDirectory)out\$(MSBuildProjectName)\</_OutputPath>
        <BaseIntermediateOutputPath>$(_OutputPath)obj</BaseIntermediateOutputPath>
        <BaseOutputPath>$(_OutputPath)bin</BaseOutputPath>
        <ResultOutputPath>$(SolutionDir)out\</ResultOutputPath>
    </PropertyGroup>
</Project>

After this, just do

  1. PS D:\git\project\src\SomeCsproj> dotnet build
  2. PS D:\git\project\src\SomeCsproj> dotnet aspnet-codegenerator -h
  3. See error output above.

Workaround:

  1. Temporarily rename the file Directory.Build.props
  2. PS D:\git\project\src\SomeCsproj> dotnet build
  3. PS D:\git\project\src\SomeCsproj> dotnet aspnet-codegenerator -h
  4. Works as expected :-)

Criticality:

I guess a lot of people out there are having this problem and do not know in the first place that it could be caused by their build customization. Therefore this might be worth a fix or maybe a better error message like. Please check if you are using build customization which is re-directing your build output or something like that.

twsouthwick commented 8 months ago

I'm hitting this now when enabling the new artifacts directory:

<ArtifactsPath>$(MSBuildThisFileDirectory)artifacts</ArtifactsPath>

For the docs on this .NET 8+ feature: https://learn.microsoft.com/en-us/dotnet/core/sdk/artifacts-output