roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

Integrate csi to dotnet cli

Open tmat opened this issue 7 years ago • 40 comments

Execute a script:

dotnet script.csx

Start C# REPL:

dotnet csi 

Similarly for F# and VB scripts.

tmat avatar Mar 09 '17 18:03 tmat

See also https://github.com/fsharp/fslang-design/issues/167

tmat avatar Mar 09 '17 18:03 tmat

might be worth a look https://github.com/filipw/dotnet-script

filipw avatar Mar 09 '17 18:03 filipw

Thanks for the pointer @filipw. Do you think we could get rid of the extra verb "script"? It doesn't add much value since we know it's script from the extension.

tmat avatar Mar 09 '17 20:03 tmat

The current extensibility around dotnet CLI forces you to have some verb, if you want dotnet.exe to pick up your extension - there is a good article here http://mattwarren.org/2016/10/03/Adding-a-verb-to-the-dotnet-CLI-tooling/.

That particular tool was created as a .NET Core app with a name dotnet-script, and as such, dotnet driver sees it as script. I think skipping it would require changes to dotnet CLI extensibility model.

There are additional guidelines here https://github.com/dotnet/cli/blob/rel/1.0.0/Documentation/developer-guide.md#adding-a-command, where they mention that "core" commands may be added to dotnet.dll directly. So in this case the functionality, being a core part of the C# compiler, could also be added straight in there, rather than use the general extensibility model.

filipw avatar Mar 09 '17 20:03 filipw

What's the status of this?

LYP951018 avatar Sep 23 '17 14:09 LYP951018

FYI dotnet-script project has been upgraded recently to support .NET Core 2.0. It is also integrated into OmniSharp in order to provide language services in Visual Studio Code.

filipw avatar Nov 02 '17 12:11 filipw

I see that the current build produces C:\Code\dotnet\roslyn\Binaries\Debug\Exes\csi\netcoreapp2.0\csi.dll. If that was renamed to dotnet-csi.dll would we then not have dotnet csi? I'm not sure there's a reason to build it into the dotnet shell itself.

adamralph avatar Nov 10 '17 20:11 adamralph

BTW - is this not a duplicate of https://github.com/dotnet/roslyn/issues/6882 ?

adamralph avatar Nov 10 '17 20:11 adamralph

Would like to see this come to light

peabnuts123 avatar Dec 05 '17 20:12 peabnuts123

dotnet csi xxx.csproj

would build the csproj then load it (and all its references) in csi. A man can dream...

Pvlerick avatar Jan 10 '18 08:01 Pvlerick

Wouldn't this be a perfect fit for a global tool now?

dotnet tool install -g dotnet-csi / dotnet tool install -g csi

dasMulli avatar Jun 23 '18 00:06 dasMulli

dotnet-script is already a global tool, and it supports everything CSI does + more (nuget etc).

in fact the syntax for nuget support was aligned with @tmat to ensure we don't invent anything non standard. it has OmniSharp support too.

what would you need a global CSI tool for? 😀

filipw avatar Jun 23 '18 04:06 filipw

dotnet-script is fine just as well. I like to mess around with interactive mode from time to time. a few minutes ago I wanted to check some decimal numbers for bit masks. which I ended up doing using mono's csi. That's what made me think "I should check on the status of this on core"

I also like csi for small syntax demos, for which it would be great to have a tool that's available everywhere (e.g. vs command prompt) but a single dotnet tool install command is probably sufficient for this as well (since it would apply to both dotnet-script and dotnet-csi using this model).

dasMulli avatar Jun 23 '18 04:06 dasMulli

dotnet-script has interactive mode too 🙂

personally I use csi for situations like you mentioned - where I want to be certain that it already exists on the target machine without any extra set up, and since it ships with msbuild tools, it's always there.

On a side note, csi also works on mono, just the default distro is missing System.Runtime.dll that has to be copied into the csi folder.

filipw avatar Jun 23 '18 07:06 filipw

That would allow us to use C# Scripts on build systems that aren't MSBuild based (like gn for example) as the script language. For now, I have to install dotnet-script from @filipw until we have a REPL or some script runner like mono used to have.

galvesribeiro avatar Feb 11 '20 15:02 galvesribeiro

With dotnet run, there's little reason to use some kind of scripting workaround. You can just use a regular C# project: https://github.com/adamralph/minver/blob/master/build.cmd

adamralph avatar Feb 11 '20 16:02 adamralph

@adamralph yeah but we don't want to mess with MSBuild are there are too much more than just build the project so we prefer the flexibility of a build script as code. We're just trying to avoid python since everything else we have is .Net anyway.

galvesribeiro avatar Feb 11 '20 16:02 galvesribeiro

@galvesribeiro I don't see what MSBuild has to do with it. All you need is:

  • the .NET SDK (which you would also need if csi were integrated into the dotnet cli)
  • a .csproj file, containing this at a minimum:
    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <RollForward>major</RollForward>
        <TargetFramework>netcoreapp3.1</TargetFramework>
      </PropertyGroup>
    </Project>
    
  • a Program.cs file

Compare this with a "script" (assuming csi were integrated into the dotnet cli):

  • the .NET SDK
  • a .csx file

So the only extra "noise" required for using dotnet run right now is a csproj file (and a few extra lines in Program.cs, but that also comes with the benefit that you can use the full power of csproj if you need it. And since it's a regular .NET project, all your favourite IDE's already work with it, and support full debugging etc.

adamralph avatar Feb 11 '20 16:02 adamralph

@adamralph ok now I see what you meant.

That was my original approach. However, parameters being passed from gn on linux are based on --param (double -) which doesn't work with dotnet run for whatever reason (args is always empty).

Also, it worth to mention that once we have the .csx with the proper shebang(#!/usr/bin/env dotnet-script), I can call the script directly without need to invoke dotnet CLI like this: ./myscript.csx --param1 --param2.

galvesribeiro avatar Feb 11 '20 16:02 galvesribeiro

The "csproj + Program.cs = Build Script" is basically what nuke (https://github.com/nuke-build/nuke) promotes and makes easy.

Note that in theory dotnet run should support passing all sorts of args after a -- separator (dotnet run -c Release -- --foo --bar) but do open isssues on dotnet/sdk if it doesn't work for you..

dasMulli avatar Feb 12 '20 05:02 dasMulli

It's also the model for https://github.com/adamralph/bullseye

adamralph avatar Feb 12 '20 08:02 adamralph

dotnet-script is a very nice project... However, I am still in favor of having a 'dotnet csi' command included in the dotnet CLI to make sure that it is being released and is compatible with all versions, including previews, of dotnet... Currently, dotnet-script is only compatible up to dotnet 3.1.0. I am working on testing dotnet preview 5.0.0 and there is no way, that I know of, to run .csx files with this version. I have mainly been working with F# and must admit that it is nice for 'dotnet fsi' to be supported for every release since dotnet 3.0 (including previews).

In addition. I would assume that adding the 'dotnet csi' command would allow the OmniSharp project to more easily integrate a C# REPL into VS Code in the same manner that the Ionide-FSharp project includes an F# REPL. See https://github.com/OmniSharp/omnisharp-vscode/issues/1239 . With all the new functional features expected in C# 9, the REPL will be very much appreciated by the group of functional first programmers that are going to start taking C# seriously as a functional friendly language, and who are used to constantly utilizing the REPL during development.

dariens avatar Jun 03 '20 19:06 dariens

so what's the plan for csi after years that fsi been release in dotnet sdk ?

John0King avatar Apr 05 '21 08:04 John0King

i have to use python for last 4 years. just because install python gives python in cmd, while installing dotnet does not gives me csi or fsi..

dzmitry-lahoda avatar Apr 16 '21 07:04 dzmitry-lahoda

dotnet gives dotnet fsi. You can use it via dotnet fsi xxx.fsx.

Happypig375 avatar Apr 16 '21 14:04 Happypig375

anything new? I see the blog that vs will use dotnet fsi in f# interactive window , so csi will only work in VS instead of cli ? https://devblogs.microsoft.com/dotnet/whats-new-in-fsharp-6/#f-tooling-net-core-the-default-for-scripting

John0King avatar Oct 20 '21 06:10 John0King

Yeah where's dotnet csi? Was surprised to see dotnet fsi included but not dotnet csi.

Regenhardt avatar Feb 17 '22 10:02 Regenhardt

I use existing csi for build scripts. Very useful. It allows to use the latest language features also, but seems like the latest library features from .NET are not available, it seems like it is still executed with .NET Framework 4. It would be nice if dotnet csi would allow to use the latest .NET 6+.

vrubleg avatar Jul 30 '22 05:07 vrubleg

@vrubleg you can switch csi to use .NET core using "#reset core"

Regenhardt avatar Jul 30 '22 06:07 Regenhardt

@Regenhardt Is it documented anywhere? It doesn't work for me: error CS1024: Preprocessor directive expected.

UPD: There is a ticket about it: https://github.com/dotnet/roslyn/issues/57939

vrubleg avatar Jul 30 '22 07:07 vrubleg