dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.65k stars 1.05k forks source link

dotnet tool install failed with DirectoryNotFoundException #40039

Open xuejiaW opened 4 months ago

xuejiaW commented 4 months ago

Describe the bug

I am following the global dotnet tool tutorial from https://learn.microsoft.com/en-us/dotnet/core/tools/global-tools-how-to-create , but i make target framework as 8.0 rather than the 6.0 in the tutorial.

After i pack the tool, i want to install it by dotnet tool install --global --add-source .\nupkg CowSay, but the System.IO.DirectoryNotFoundException exception thrown.

❯ dotnet tool install --global --add-source .\nupkg CowSay
Unhandled exception: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\wxjwa\.dotnet\tools\.store\cowsay\1.1.19\cowsay\1.1.19\tools'.
   at System.IO.Enumeration.FileSystemEnumerator`1.CreateDirectoryHandle(String path, Boolean ignoreNotFound)
   at System.IO.Enumeration.FileSystemEnumerator`1.Init()
   at System.IO.Enumeration.FileSystemEnumerable`1..ctor(String directory, FindTransform transform, EnumerationOptions options, Boolean isNormalized)
   at System.IO.Enumeration.FileSystemEnumerableFactory.UserDirectories(String directory, String expression, EnumerationOptions options)
   at System.IO.Directory.InternalEnumeratePaths(String path, String searchPattern, SearchTarget searchTarget, EnumerationOptions options)
   at System.IO.Directory.GetDirectories(String path, String searchPattern, EnumerationOptions enumerationOptions)
   at Microsoft.DotNet.ToolPackage.ToolPackageInstance..ctor(PackageId id, NuGetVersion version, DirectoryPath packageDirectory, DirectoryPath assetsJsonParentDirectory)
   at Microsoft.DotNet.Cli.ToolPackage.ToolPackageDownloader.<>c__DisplayClass8_0.<InstallPackage>b__0()
   at Microsoft.DotNet.Cli.TransactionalAction.Run[T](Func`1 action, Action commit, Action rollback)
   at Microsoft.DotNet.Tools.Tool.Install.ToolInstallGlobalOrToolPathCommand.<>c__DisplayClass19_0.<Execute>b__1()
   at Microsoft.DotNet.Tools.Tool.Install.ToolInstallGlobalOrToolPathCommand.RunWithHandlingInstallError(Action installAction)
   at Microsoft.DotNet.Tools.Tool.Install.ToolInstallGlobalOrToolPathCommand.Execute()
   at System.CommandLine.Invocation.InvocationPipeline.Invoke(ParseResult parseResult)
   at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, TimeSpan startupTime, ITelemetry telemetryClient)

The full dotnet info is :

❯ dotnet --info
.NET SDK:
 Version:           8.0.203
 Commit:            5e1ceea679
 Workload version:  8.0.200-manifests.4e94be9c

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22631
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.203\

.NET workloads installed:
There are no installed workloads to display.

Host:
  Version:      8.0.3
  Architecture: x64
  Commit:       9f4b1f5d66

.NET SDKs installed:
  7.0.407 [C:\Program Files\dotnet\sdk]
  8.0.203 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 7.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 7.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 7.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  None

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

The csproj is:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <PackAsTool>true</PackAsTool>
        <ToolCommandName>cowsay</ToolCommandName>
        <PackageOutputPath>./nupkg</PackageOutputPath>
        <Version> 1.0.1 </Version>
    </PropertyGroup>

</Project>

i am sure the nupkg has been created:

├── nupkg
│   └── CowSay.1.0.1.nupkg

If i install the tool with version, i.e, dotnet tool install --global --add-source .\nupkg CowSay --version 1.0.1, it works. But the version option should not be necessary. I have tried in .net 7, the install command can succeed without --version

mdepa commented 4 months ago

Hi, I'm facing the same issue performing the dotnet tool install into a running container to build in gitlab.

In my case I see the issue with dotnet sdk 6.0.417-focal. I spent a lot of time trying to figure out what's the issue and it seems to me that the dotnet tool install command is executed before the dotnet pack completes as the pack was executed in background and not synchronously when the cli completes.

I was able to workaround the issue performing a "dotnet build-server shutdown" in between the dotnet pack and the dotnet install commands: maybe it's unrelated, maybe it's just creating a delay that permits to the dotnet pack command to complete, however, to me, it mitigated the issue unblocking the pipeline development.

Forgind commented 1 week ago

It seems like the instructions changed to making microsoft.botsay instead of CowSay, but I'm assuming that isn't relevant.

I made microsoft.botsay per the instructions except that I changed the framework to net8.0 instead of net6.0 as you did. I packed it then tried to install it globally, and it worked properly (with my 9.0 preview).

Then I deleted my .dotnet folder so that most of the path would be absent, and it just recreated it without a problem.

I tried the same with 8.0.108 and 8.0.400, and neither of those seemed to have a problem.

Is there anything I did wrong with trying to reproduce this (other than using a slightly different version of dotnet)?