2

(With Visual Studio 17.14.13) Steps to reproduce (sorry this is a bit long but these are the fewest steps I could find):

  1. From the command line, create the solution folder:
    • md test1
    • cd test1
  2. Create an empty solution:
    • dotnet new sln --name test1 --format slnx
  3. Create a razor class library project:
    • dotnet new razorclasslib -n test1 -o .
  4. Add the razor class library project to solution:
    • dotnet sln add test1.csproj
  5. Add TypeScript support:
    • npm install typescript --save-dev
  6. Open the test1 solution.
  7. Add a typescript file to the test1 project:
    1. Right-click test1 project and select "Add | New Item"
    2. Select "Web | Scripts" on the left.
    3. Select "TypeScript File" on the right.
    4. Click "Add" to use the default name of "file1.ts".
    5. Add the following TypeScript code to "file1.ts":
    export function showAlert(message: string): void
    {
       alert(message);
    }
    
  8. Add the typescript NuGet package to the "test1.csproj" project file:
    <PackageReference Include="Microsoft.TypeScript.MSBuild" Version="5.9.2">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    
  9. Add a tsconfig.json configuration file to the root of the project.
    (Menu item is under Add -> New Item, C# Items -> Web -> Scripts -> TypeScript JSON Configuration File.)
    The file contents are:
    {
      "compilerOptions": {
        "target": "ES6",
        "module": "ES6",
        "outDir": "./wwwroot/js",
        "strict": true
      },
      "include": [ "*.ts" ]
    }
    

Build/Rebuild

Now select "Build | Rebuild test1" twice.

The first rebuild succeeds, but the second rebuild results in an error message:

System.InvalidOperationException: No file exists for the asset at either location 'D:code\test1\wwwroot\js\file1.js' or 'wwwroot\js\file1.js'. at Microsoft.AspNetCore.StaticWebAssets.Tasks.StaticWebAsset.ResolveFile(String identity, String originalItemSpec) at Microsoft.AspNetCore.StaticWebAssets.Tasks.DefineStaticWebAssets.ResolveFileDetails(String originalItemSpec, String identity) at Microsoft.AspNetCore.StaticWebAssets.Tasks.DefineStaticWebAssets.Execute()

I can then repeatedly select "Build | Rebuild test1" and it will fail on alternate rebuilds.

My questions are:

  1. What have I done wrong?
  2. How do I fix it?
  3. Can anyone else even reproduce this?

2 Answers 2

2

This should be a bug, so you did nothing wrong. I've found 2 related issues 1 2, both of which indicate that this error only appeared after a .net9 version update last year. I can reproduce this in .net 9.0.301, but this is OK in .net 8.0.119, and it seems that excluding this folder from the build pipeline can make the rebuild command successful.

<ItemGroup>
    <Content Remove="wwwroot\js\**" />
    <None Include="wwwroot\js\**" />
</ItemGroup>
Sign up to request clarification or add additional context in comments.

Comments

0

This is caused by the new static web asset build system.

<PropertyGroup>
    <StaticWebAssetsEnabled>false</StaticWebAssetsEnabled>
</PropertyGroup>

<StaticWebAssetsEnabled>false</StaticWebAssetsEnabled> disables automatic discovery and management of static files (CSS, JS, images, etc.), removes the build-time cataloging that's causing your timing error, and falls back to the older, simpler static file serving approach

Pros:

  • Fixes your timing issue completely
  • Simpler build process
  • No more static web asset errors

Cons:

Lose some optimizations and features:

  • Automatic bundling coordination
  • Better caching headers
  • Integration with libraries that use static web assets
  • Some tooling integration

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.