dotnet-script / dotnet-script

Run C# scripts from the .NET CLI.
MIT License
2.67k stars 162 forks source link

Shebang directive on top of the file breaks #r SDK parsing #723

Open marvfinsy opened 1 year ago

marvfinsy commented 1 year ago

In the Specifying an sdk u show an example creating a web app:

r "sdk:Microsoft.NET.Sdk.Web"

using Microsoft.AspNetCore.Builder;

var a = WebApplication.Create(); a.MapGet("/", () => "Hello world"); a.Run();

====

I get error saying 'AspNetCore' does not exist in namespace 'Microsoft'.

Has this changed?

environment: Mac osx ARM i have Core6 and Core7 Frameworks installed. Currently using Core7.

I created project using command as shown in doc $> dotnet script init

thx ~Marvin Foster mmarvb7@gmail.com or marvinfoster@finsync.com

vladslavin commented 1 year ago

Same issue, SDK is installed 🤷‍♂️

Version             : 1.4.0
Install location    : /Users/slavin/.dotnet/tools/.store/dotnet-script/1.4.0/dotnet-script/1.4.0/tools/net7.0/any
Target framework    : net7.0
.NET Core version   : 7.0.5
Platform identifier : osx
Runtime identifier  : osx-x64
filipw commented 1 year ago

it works on my machine™️ 😀 please run dotnet script {your csx file} --verbosity d --no-cache and post the output here.

vladslavin commented 1 year ago

➜ dotnet script index.csx --verbosity d --no-cache

dbug: Dotnet.Script.Core.Commands.ExecuteScriptCommand[0]
      The script /Users/slavin/source/test/index.csx was executed with the '--no-cache' flag. Skipping cache.
info: Dotnet.Script.Core.ScriptCompiler[0]
      Current runtime is 'osx'.
dbug: Dotnet.Script.DependencyModel.ProjectSystem.ScriptProjectProvider[0]
      Creating project file for /Users/slavin/source/test/index.csx
dbug: Dotnet.Script.DependencyModel.ProjectSystem.ScriptParser[0]
      Parsing /Users/slavin/source/test/index.csx
dbug: Dotnet.Script.DependencyModel.ProjectSystem.ScriptProjectProvider[0]
      Project file saved to /Users/slavin/Library/Caches/dotnet-script/Users/slavin/source/test/net7.0/script.csproj
dbug: Dotnet.Script.DependencyModel.ProjectSystem.ScriptProjectProvider[0]
      <?xml version="1.0" encoding="utf-8"?>
      <Project Sdk="Microsoft.NET.Sdk">
        <PropertyGroup>
          <OutputType>Exe</OutputType>
          <TargetFramework>net7.0</TargetFramework>
          <LangVersion>latest</LangVersion>
        </PropertyGroup>
        <ItemGroup></ItemGroup>
        <Target Name="RecordReferencePaths" AfterTargets="AfterResolveReferences">
          <WriteLinesToFile File="$(OutputPath)/ReferencePaths.txt" Lines="@(ReferencePath)" />
        </Target>
      </Project>
dbug: Dotnet.Script.DependencyModel.Context.DotnetRestorer[0]
      Restoring /Users/slavin/Library/Caches/dotnet-script/Users/slavin/source/test/net7.0/script.csproj using the dotnet cli. RuntimeIdentifier : osx-x64 NugetConfigFile: /Users/slavin/.nuget/NuGet/NuGet.Config
dbug: Dotnet.Script.DependencyModel.Context.ProfiledRestorer[0]
      Restoring /Users/slavin/Library/Caches/dotnet-script/Users/slavin/source/test/net7.0/script.csproj took 1097ms
dbug: Dotnet.Script.Core.ScriptCompiler[0]
      Configuration/Optimization mode: Debug
/Users/slavin/source/test/index.csx(5,17): error CS0234: The type or namespace name 'AspNetCore' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
/Users/slavin/source/test/index.csx(7,9): error CS0103: The name 'WebApplication' does not exist in the current context
filipw commented 1 year ago

do you have the #r "sdk:Microsoft.NET.Sdk.Web" directive on top of your file?

according to the logs, the script uses <Project Sdk="Microsoft.NET.Sdk"> instead of <Project Sdk="Microsoft.NET.Sdk.Web"> which is why the ASP.NET Core dependencies cannot be found

vladslavin commented 1 year ago

@filipw yes, there seem to be some kind of miss match happening. Here's the content of the script:

#!/usr/bin/env dotnet-script

#r "sdk:Microsoft.NET.Sdk.Web"

using Microsoft.AspNetCore.Builder;

var a = WebApplication.Create();
a.MapGet("/", () => "Hello world");
a.Run();
filipw commented 1 year ago

oh I see the bug now. it's the shebang on top that breaks it. if you remove #!/usr/bin/env dotnet-script it will work.

Likely a parser problem.

vladslavin commented 1 year ago

oh I see the bug now. it's the shebang on top that breaks it. if you remove #!/usr/bin/env dotnet-script it will work.

Likely a parser problem.

thanks Filip, that's exactly it 👍

mmarvb8h commented 1 year ago

I finally got a chance to get back to this. Yes that seems to be the problem. Also i noticed that the "sdk directive" must be on the first line of the script. If i skip a line putting it on line 2, i also get the AspCore dependencies not found.

Noticed in Visual Code Intellisense complains those dependencies are not available but the code does run. Is that because the enable is missing from the .csproj file? I have another dotnet app that i removed that from the .csproj and received the same intellisense warnings but the app. runs. so i assume its the same thing.

Is there a template that's used that i can add this so when the .csproj file is being built that tag will be there?

AraHaan commented 1 year ago

I would like the winforms / wpf sdk to be usable as well with this.

alphajoza commented 2 months ago

Any fix for losing Intellisense and code completion when referencing a sdk?

2024-05-05_11-53