Giter Site home page Giter Site logo

dotnet-script / dotnet-script Goto Github PK

View Code? Open in Web Editor NEW
2.6K 56.0 160.0 8.01 MB

Run C# scripts from the .NET CLI.

License: MIT License

C# 98.50% Batchfile 0.01% Shell 0.68% PowerShell 0.63% Dockerfile 0.18%
csx roslyn csharp-script csi dotnet-script

dotnet-script's Introduction

dotnet script

Run C# scripts from the .NET CLI, define NuGet packages inline and edit/debug them in VS Code - all of that with full language services support from OmniSharp.

Build status

Build Status

NuGet Packages

Name Version Framework(s)
dotnet-script (global tool) Nuget net6.0,net7.0,net8.0
Dotnet.Script (CLI as Nuget) Nuget net6.0,net7.0,net8.0
Dotnet.Script.Core Nuget net6.0,net7.0,net8.0,netstandard2.0
Dotnet.Script.DependencyModel Nuget netstandard2.0
Dotnet.Script.DependencyModel.Nuget Nuget netstandard2.0

Installing

Prerequisites

The only thing we need to install is .NET 6.0, .NET 7.0 or .NET 8.0 SDK.

.NET Core Global Tool

.NET Core 2.1 introduced the concept of global tools meaning that you can install dotnet-script using nothing but the .NET CLI.

dotnet tool install -g dotnet-script

You can invoke the tool using the following command: dotnet-script
Tool 'dotnet-script' (version '0.22.0') was successfully installed.

The advantage of this approach is that you can use the same command for installation across all platforms. .NET Core SDK also supports viewing a list of installed tools and their uninstallation.

dotnet tool list -g

Package Id         Version      Commands
---------------------------------------------
dotnet-script      0.22.0       dotnet-script
dotnet tool uninstall dotnet-script -g

Tool 'dotnet-script' (version '0.22.0') was successfully uninstalled.

Windows

PowerShell script for installation.

(new-object Net.WebClient).DownloadString("https://raw.githubusercontent.com/dotnet-script/dotnet-script/master/install/install.ps1") | iex

Linux and Mac

curl -s https://raw.githubusercontent.com/dotnet-script/dotnet-script/master/install/install.sh | bash

If permission is denied we can try with sudo

curl -s https://raw.githubusercontent.com/dotnet-script/dotnet-script/master/install/install.sh | sudo bash

Docker

A Dockerfile for running dotnet-script in a Linux container is available. Build:

cd build
docker build -t dotnet-script -f Dockerfile ..

And run:

docker run -it dotnet-script --version

Github

You can manually download all the releases in zip format from the GitHub releases page.

Usage

Our typical helloworld.csx might look like this:

Console.WriteLine("Hello world!");

That is all it takes and we can execute the script. Args are accessible via the global Args array.

dotnet script helloworld.csx

The following namespaces are available in the script implicitly and do not need to be imported explicitly:

  • System
  • System.IO
  • System.Collections.Generic
  • System.Console
  • System.Diagnostics
  • System.Dynamic
  • System.Linq
  • System.Linq.Expressions
  • System.Text
  • System.Threading.Tasks

Scaffolding

Simply create a folder somewhere on your system and issue the following command.

dotnet script init

This will create main.csx along with the launch configuration needed to debug the script in VS Code.

.
├── .vscode
│   └── launch.json
├── main.csx
└── omnisharp.json

We can also initialize a folder using a custom filename.

dotnet script init custom.csx

Instead of main.csx which is the default, we now have a file named custom.csx.

.
├── .vscode
│   └── launch.json
├── custom.csx
└── omnisharp.json

Note: Executing dotnet script init inside a folder that already contains one or more script files will not create the main.csx file.

Running scripts

You can execute your script using dotnet script or dotnet-script.

dotnet script foo.csx
dotnet-script foo.csx

On OSX/Linux, scripts can be executed directly from a shell as if they were executables.

foo.csx arg1 arg2 arg3

OSX/Linux

Just like all scripts, on OSX/Linux you need to have a #! and mark the file as executable via chmod +x foo.csx. If you use dotnet script init to create your csx it will automatically have the #! directive and be marked as executable.

The OSX/Linux shebang directive should be #!/usr/bin/env dotnet-script

#!/usr/bin/env dotnet-script
Console.WriteLine("Hello world");

On Windows, you can run dotnet script register to achieve a similar behaviour. This registers dotnet-script in the Windows registry as the tool to process .csx files.

You can pass arguments to control your script execution more.

foo.csx arg1 arg2 arg3
dotnet script foo.csx -- arg1 arg2 arg3
dotnet-script foo.csx -- arg1 arg2 arg3

Passing arguments to scripts

All arguments after -- are passed to the script in the following way:

dotnet script foo.csx -- arg1 arg2 arg3

Then you can access the arguments in the script context using the global Args collection:

foreach (var arg in Args)
{
    Console.WriteLine(arg);
}

All arguments before -- are processed by dotnet script. For example, the following command-line

dotnet script -d foo.csx -- -d

will pass the -d before -- to dotnet script and enable the debug mode whereas the -d after -- is passed to script for its own interpretation of the argument.

NuGet Packages

dotnet script has built-in support for referencing NuGet packages directly from within the script.

#r "nuget: AutoMapper, 6.1.0"

package

Note: Omnisharp needs to be restarted after adding a new package reference

Package Sources

We can define package sources using a NuGet.Config file in the script root folder. In addition to being used during execution of the script, it will also be used by OmniSharp that provides language services for packages resolved from these package sources.

As an alternative to maintaining a local NuGet.Config file we can define these package sources globally either at the user level or at the computer level as described in Configuring NuGet Behaviour

It is also possible to specify packages sources when executing the script.

dotnet script foo.csx -s https://SomePackageSource

Multiple packages sources can be specified like this:

dotnet script foo.csx -s https://SomePackageSource -s https://AnotherPackageSource

Creating DLLs or Exes from a CSX file

Dotnet-Script can create a standalone executable or DLL for your script.

Switch Long switch description
-o --output Directory where the published executable should be placed. Defaults to a 'publish' folder in the current directory.
-n --name The name for the generated DLL (executable not supported at this time). Defaults to the name of the script.
--dll Publish to a .dll instead of an executable.
-c --configuration Configuration to use for publishing the script [Release/Debug]. Default is "Debug"
-d --debug Enables debug output.
-r --runtime The runtime used when publishing the self contained executable. Defaults to your current runtime.

The executable you can run directly independent of dotnet install, while the DLL can be run using the dotnet CLI like this:

dotnet script exec {path_to_dll} -- arg1 arg2

Caching

We provide two types of caching, the dependency cache and the execution cache which is explained in detail below. In order for any of these caches to be enabled, it is required that all NuGet package references are specified using an exact version number. The reason for this constraint is that we need to make sure that we don't execute a script with a stale dependency graph.

Dependency Cache

In order to resolve the dependencies for a script, a dotnet restore is executed under the hood to produce a project.assets.json file from which we can figure out all the dependencies we need to add to the compilation. This is an out-of-process operation and represents a significant overhead to the script execution. So this cache works by looking at all the dependencies specified in the script(s) either in the form of NuGet package references or assembly file references. If these dependencies matches the dependencies from the last script execution, we skip the restore and read the dependencies from the already generated project.assets.json file. If any of the dependencies has changed, we must restore again to obtain the new dependency graph.

Execution cache

In order to execute a script it needs to be compiled first and since that is a CPU and time consuming operation, we make sure that we only compile when the source code has changed. This works by creating a SHA256 hash from all the script files involved in the execution. This hash is written to a temporary location along with the DLL that represents the result of the script compilation. When a script is executed the hash is computed and compared with the hash from the previous compilation. If they match there is no need to recompile and we run from the already compiled DLL. If the hashes don't match, the cache is invalidated and we recompile.

You can override this automatic caching by passing --no-cache flag, which will bypass both caches and cause dependency resolution and script compilation to happen every time we execute the script.

Cache Location

The temporary location used for caches is a sub-directory named dotnet-script under (in order of priority):

  1. The path specified for the value of the environment variable named DOTNET_SCRIPT_CACHE_LOCATION, if defined and value is not empty.
  2. Linux distributions only: $XDG_CACHE_HOME if defined otherwise $HOME/.cache
  3. macOS only: ~/Library/Caches
  4. The value returned by Path.GetTempPath for the platform.

Debugging

The days of debugging scripts using Console.WriteLine are over. One major feature of dotnet script is the ability to debug scripts directly in VS Code. Just set a breakpoint anywhere in your script file(s) and hit F5(start debugging)

debug

Script Packages

Script packages are a way of organizing reusable scripts into NuGet packages that can be consumed by other scripts. This means that we now can leverage scripting infrastructure without the need for any kind of bootstrapping.

Creating a script package

A script package is just a regular NuGet package that contains script files inside the content or contentFiles folder.

The following example shows how the scripts are laid out inside the NuGet package according to the standard convention .

└── contentFiles
    └── csx
        └── netstandard2.0
            └── main.csx

This example contains just the main.csx file in the root folder, but packages may have multiple script files either in the root folder or in subfolders below the root folder.

When loading a script package we will look for an entry point script to be loaded. This entry point script is identified by one of the following.

  • A script called main.csx in the root folder
  • A single script file in the root folder

If the entry point script cannot be determined, we will simply load all the scripts files in the package.

The advantage with using an entry point script is that we can control loading other scripts from the package.

Consuming a script package

To consume a script package all we need to do specify the NuGet package in the #loaddirective.

The following example loads the simple-targets package that contains script files to be included in our script.

#load "nuget:simple-targets-csx, 6.0.0"

using static SimpleTargets;
var targets = new TargetDictionary();

targets.Add("default", () => Console.WriteLine("Hello, world!"));

Run(Args, targets);

Note: Debugging also works for script packages so that we can easily step into the scripts that are brought in using the #load directive.

Remote Scripts

Scripts don't actually have to exist locally on the machine. We can also execute scripts that are made available on an http(s) endpoint.

This means that we can create a Gist on Github and execute it just by providing the URL to the Gist.

This Gist contains a script that prints out "Hello World"

We can execute the script like this

dotnet script https://gist.githubusercontent.com/seesharper/5d6859509ea8364a1fdf66bbf5b7923d/raw/0a32bac2c3ea807f9379a38e251d93e39c8131cb/HelloWorld.csx

That is a pretty long URL, so why don't make it a TinyURL like this:

dotnet script https://tinyurl.com/y8cda9zt

Script Location

A pretty common scenario is that we have logic that is relative to the script path. We don't want to require the user to be in a certain directory for these paths to resolve correctly so here is how to provide the script path and the script folder regardless of the current working directory.

public static string GetScriptPath([CallerFilePath] string path = null) => path;
public static string GetScriptFolder([CallerFilePath] string path = null) => Path.GetDirectoryName(path);

Tip: Put these methods as top level methods in a separate script file and #load that file wherever access to the script path and/or folder is needed.

REPL

This release contains a C# REPL (Read-Evaluate-Print-Loop). The REPL mode ("interactive mode") is started by executing dotnet-script without any arguments.

The interactive mode allows you to supply individual C# code blocks and have them executed as soon as you press Enter. The REPL is configured with the same default set of assembly references and using statements as regular CSX script execution.

Basic usage

Once dotnet-script starts you will see a prompt for input. You can start typing C# code there.

~$ dotnet script
> var x = 1;
> x+x
2

If you submit an unterminated expression into the REPL (no ; at the end), it will be evaluated and the result will be serialized using a formatter and printed in the output. This is a bit more interesting than just calling ToString() on the object, because it attempts to capture the actual structure of the object. For example:

~$ dotnet script
> var x = new List<string>();
> x.Add("foo");
> x
List<string>(1) { "foo" }
> x.Add("bar");
> x
List<string>(2) { "foo", "bar" }
>

Inline Nuget packages

REPL also supports inline Nuget packages - meaning the Nuget packages can be installed into the REPL from within the REPL. This is done via our #r and #load from Nuget support and uses identical syntax.

~$ dotnet script
> #r "nuget: Automapper, 6.1.1"
> using AutoMapper;
> typeof(MapperConfiguration)
[AutoMapper.MapperConfiguration]
> #load "nuget: simple-targets-csx, 6.0.0";
> using static SimpleTargets;
> typeof(TargetDictionary)
[Submission#0+SimpleTargets+TargetDictionary]

Multiline mode

Using Roslyn syntax parsing, we also support multiline REPL mode. This means that if you have an uncompleted code block and press Enter, we will automatically enter the multiline mode. The mode is indicated by the * character. This is particularly useful for declaring classes and other more complex constructs.

~$ dotnet script
> class Foo {
* public string Bar {get; set;}
* }
> var foo = new Foo();

REPL commands

Aside from the regular C# script code, you can invoke the following commands (directives) from within the REPL:

Command Description
#load Load a script into the REPL (same as #load usage in CSX)
#r Load an assembly into the REPL (same as #r usage in CSX)
#reset Reset the REPL back to initial state (without restarting it)
#cls Clear the console screen without resetting the REPL state
#exit Exits the REPL

Seeding REPL with a script

You can execute a CSX script and, at the end of it, drop yourself into the context of the REPL. This way, the REPL becomes "seeded" with your code - all the classes, methods or variables are available in the REPL context. This is achieved by running a script with an -i flag.

For example, given the following CSX script:

var msg = "Hello World";
Console.WriteLine(msg);

When you run this with the -i flag, Hello World is printed, REPL starts and msg variable is available in the REPL context.

~$ dotnet script foo.csx -i
Hello World
>

You can also seed the REPL from inside the REPL - at any point - by invoking a #load directive pointed at a specific file. For example:

~$ dotnet script
> #load "foo.csx"
Hello World
>

Piping

The following example shows how we can pipe data in and out of a script.

The UpperCase.csx script simply converts the standard input to upper case and writes it back out to standard output.

using (var streamReader = new StreamReader(Console.OpenStandardInput()))
{
    Write(streamReader.ReadToEnd().ToUpper());
}

We can now simply pipe the output from one command into our script like this.

echo "This is some text" | dotnet script UpperCase.csx
THIS IS SOME TEXT

Debugging

The first thing we need to do add the following to the launch.config file that allows VS Code to debug a running process.

{
    "name": ".NET Core Attach",
    "type": "coreclr",
    "request": "attach",
    "processId": "${command:pickProcess}"
}

To debug this script we need a way to attach the debugger in VS Code and the simplest thing we can do here is to wait for the debugger to attach by adding this method somewhere.

public static void WaitForDebugger()
{
    Console.WriteLine("Attach Debugger (VS Code)");
    while(!Debugger.IsAttached)
    {
    }
}

To debug the script when executing it from the command line we can do something like

WaitForDebugger();
using (var streamReader = new StreamReader(Console.OpenStandardInput()))
{
    Write(streamReader.ReadToEnd().ToUpper()); // <- SET BREAKPOINT HERE
}

Now when we run the script from the command line we will get

$ echo "This is some text" | dotnet script UpperCase.csx
Attach Debugger (VS Code)

This now gives us a chance to attach the debugger before stepping into the script and from VS Code, select the .NET Core Attach debugger and pick the process that represents the executing script.

Once that is done we should see our breakpoint being hit.

Configuration(Debug/Release)

By default, scripts will be compiled using the debug configuration. This is to ensure that we can debug a script in VS Code as well as attaching a debugger for long running scripts.

There are however situations where we might need to execute a script that is compiled with the release configuration. For instance, running benchmarks using BenchmarkDotNet is not possible unless the script is compiled with the release configuration.

We can specify this when executing the script.

dotnet script foo.csx -c release

Nullable reference types

Starting from version 0.50.0, dotnet-script supports .Net Core 3.0 and all the C# 8 features. The way we deal with nullable references types in dotnet-script is that we turn every warning related to nullable reference types into compiler errors. This means every warning between CS8600 and CS8655 are treated as an error when compiling the script.

Nullable references types are turned off by default and the way we enable it is using the #nullable enable compiler directive. This means that existing scripts will continue to work, but we can now opt-in on this new feature.

#!/usr/bin/env dotnet-script

#nullable enable

string name = null;

Trying to execute the script will result in the following error

main.csx(5,15): error CS8625: Cannot convert null literal to non-nullable reference type.

We will also see this when working with scripts in VS Code under the problems panel.

image

Specifying an SDK

Starting with dotnet-script 1.4.0 we can now specify the SDK to be used for a script.

For instance, creating a web server in a script is now as simple as the following.

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

using Microsoft.AspNetCore.Builder;

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

Please note the the only SDK currently supported is Microsoft.NET.Sdk.Web

Team

License

dotnet-script's People

Contributors

adamralph avatar andmos avatar atifaziz avatar badbort avatar bjorkstromm avatar bndkpntr avatar cbielstein avatar danielegbers avatar dependabot[bot] avatar devlead avatar filipw avatar fredericv-unity3d avatar fredpointzero avatar georgetsiokos avatar hellotaotao avatar hjerpbakk avatar hrumhurum avatar isaacvale avatar jeremyvignelles avatar justinpealing avatar maartenba avatar maximetinu avatar mrtristan avatar nodew avatar oceania2018 avatar omidkrad avatar seesharper avatar sharpiro avatar slowlogicboy avatar zekrotja avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dotnet-script's Issues

The road towards NetCoreApp2.0

Creating this issue so that we have a place to discuss how to move dotnet script to .Net Core 2.0

In order to support a wider range of NuGet packages, we need to run dotnet script as a netcoreapp2.0

The execution side, dotnet scriptis pretty much working as can be seen in the https://github.com/filipw/dotnet-script/tree/feature/netcore20 branch.

The challenge here is that we also need to update the NuGet package reference support in OmniSharp.Roslyn, so that we can provide proper intellisense for netcoreapp2.0 based scripts.

Version 0.13.0 of dotnet scriptis based upon the new csproj project format where as OmniSharp.Roslyn uses the old project.json format under the hood for resolving NuGet package dependencies.

All the stuff related to resolving dependencies are now in Dotnet.Script.Core and I think we need to split that into a project structure that makes it more reusable from the OmniSharp side of things.

Maybe something like this

  • Dotnet.Script.Dependencies (common library for both dotnet script and OmniSharp.Roslyn)
  • Dotnet.Script.Dependencies.Runtime (used by dotnet script for resolving runtime dependencies)
  • Dotnet.Script.Dependencies.Compiletime (used by OmniSharp.Roslyn to provide compilation metadata references)

to be continued...

Passing arguments to dotnet script at the command line doesn't work.

I have the following foo.csx file (from the example in README.md):

#! "netcoreapp1.1"
#r "nuget:NetStandard.Library,1.6.1"

foreach (var arg in Args)
{
    Console.WriteLine(arg);
}

When I try to run dotnet script at the command line I see the following:

>  dotnet script .\foo.csx -- arg1 arg2 arg3
Couldn't find file '.\foo.csx -- arg1 arg2 arg3'

I've tried this from cmd and PowerShell.

I am running version 0.13.0 of dotnet-script on Windows 10.

More intuitive command-line usage

Current usage:

Usage:  [arguments] [options]

Arguments:
  script  Path to CSX script

Options:
  -a |--arg <args>                     Arguments to pass to a script. Multiple values supported
  -c |--configuration <configuration>  Configuration to use. Defaults to 'Release'
  -d | --debug                         Enables debug output.
  -? | -h | --help                     Show help information

Using -a for script arguments is awkward at best. Consider instead a more natural order:

dotnet script OPTIONS SCRIPT-PATH SCRIPT-ARGS

All args after the script path should be just passed to the script.

e.g.

dotnet script --debug ~/scripts/test.csx foo bar baz --verbose

The script will see the args foo, bar, baz & --verbose.

Add REPL

Should be simple enough with current state of Roslyn APIs.

Could not load Newtonsoft.Json assembly

I'm using .Net Core 1.1.4: https://github.com/dotnet/core/blob/master/release-notes/download-archives/1.1.4-download.md

I downloaded 0.13 of dotnet-script and I can't seem to figure out why I'm getting the error below. I've tried including a different nuget reference of #r "nuget:Newtonsoft.Json, 10.0.0".

Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621)
   at Auth0.ManagementApi.Clients.ConnectionsClient.GetAllAsync(String strategy, String fields, Boolean includeFields, String name)
   at Submission#0.<<Initialize>>d__0.MoveNext() in /Users/saqibrokadia/invio/scripts/auth0/auth/helloworld.csx:line 9
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.<RunSubmissionsAsync>d__9`1.MoveNext()
Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621)

I think this has to do with dependency resolution of nuget, but feels like I'm pretty stuck here and not sure what else to try. I've read through other issues in this repository, but nothing seems to fix this.

#! "netcoreapp1.0"
#r "~/.nuget/packages/Newtonsoft.Json/10.0.3/lib/netstandard1.0/Newtonsoft.Json.dll"
#r "nuget:Auth0.ManagementApi, 4.4.0"

using Auth0.ManagementApi;

var client = new ManagementApiClient("token", new Uri("https://foo.auth0.com/api/v2/"));

Console.WriteLine(await client.Connections.GetAllAsync("auth0"));

Consider "self-contained" scripts

Nice to see a new dawn for C# scripting :)

I am not entirely sure that project.json is the best way to specify dependencies since Microsoft is moving away from it. It would be really nice if the script was self-contained in the sense that we could use load directives for all external code and not only for other script files.

I did a spike for this a while back and most of the Nuget API stuff is ready for .Net Core so at least it should be doable.

That way we could just execute a script without the need for a second file to describe the dependencies.

Something like this maybe

#load "nuget:somepackageid"

Add Dockerfile for dotnet-script

As discussed with @seesharper, a simple Dockerfile and image on the Docker Hub will make it easy to run dotnet scripts without installing anything, as well as being platform neutral. It would be nice to use scripts written in dotnet as part of a Docker container pipeline. I can take a look at this, I guess it will streamline the install on Linux in the process.

Align references / imports with CSI and Interactive

This is what is used by both:

  • CSI
  • Interactive window

as "seed".

/r:System
/r:System.Core
/r:Microsoft.CSharp
/r:System.ValueTuple.dll  // this one is new in VS 2017
/u:System
/u:System.IO
/u:System.Collections.Generic
/u:System.Console
/u:System.Diagnostics
/u:System.Dynamic
/u:System.Linq
/u:System.Linq.Expressions
/u:System.Text
/u:System.Threading.Tasks

We should align the scripting experience to match this.

Add integration tests

We should have some integration tests running against different types of CSX files.

Release 0.8.0

Need to get 0.8.0 out - plenty of good changes pending.

Native image cannot be loaded multiple times

I am using dotnet-script for my project. And getting this Error msg:

Native image cannot be loaded multiple times

This Error also occurs on Linux (Debian), and I cannot use .net Framework because the project has to run on a raspberry.
I tried NETCore 1.0 and Dotnet.Script 0.7.0-beta too, same issue.
Everything else is working fine but as soon as "IPAdress.Parse("")" gets called outside of the Script (inside the .dll) the error will occure.
If I call "IPAdress.Parse("")" inside an async Task<>, it works fine.

Very Simple to reproduce:

//foo.csx
using ConsoleApp1;
var mc = new MyClass("127.0.0.1");
//project.json
{
  "dependencies": {
    "ConsoleApp1": "1.0.0-*"
  },
  "frameworks": {
    "netcoreapp1.1": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.1.0"
        }
      },
      "imports": "dnxcore50"
    }
  },
  "tools": {
    "Dotnet.Script": {
      "version": "0.8.0-beta",
      "imports": [
        "portable-net45+win8",
        "dnxcore50"
      ]
    },
  }
}
//MyClass.cs
using System.Net;

namespace ConsoleApp1
{
    public class MyClass
    {
        public MyClass(string ip)
        {
            var m_ip = IPAddress.Parse(ip);
        }
    }
}
//project.json
{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable"
  },
  "dependencies": {
    "Verbosity": "1.0.0-*"
  },
  "frameworks": {
    "netcoreapp1.1": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.1.0"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

2 more fast questions:
1.
From the Read.me:
<NUGET_ROOT>\Dotnet.Script\0.7.0-beta\lib\netcoreapp1.0\dotnet-script.deps.json
this file doesnt exist on my machine.
2.
The Script looking for .dll's in the bin/Release instead of bin/Debug directory. Is there a way to change that?

Extract Dotnet.Script.Runner package

I'd split the CLI package and the runner package.

The runner package could be useful to anyone who wants to go from project.json => executable script easily, and support debugging scenarios too, which is a fair amount.

Debugging scripts loaded in via #load directive

I no longer have access to older versions of dotnet that use project.json so i modified your source to just ignore the part where it checks for it so that I could use just a .csx file w/ no project.json/csproj and got the debugging working w/ c#7. You can see that change here

So my issue is that if load in another script from the main debuggable script via #load, I get the following issue when setting a breakpoint in that loaded in script:

WARNING: Could not load symbols for 'ℛ*c9b5c793-2995-47ba-9ae3-b0c8d54c3bdf#1-0'. 'C:\Program Files\dotnet\ℛ*c9b5c793-2995-47ba-9ae3-b0c8d54c3bdf#1-0.pdb' could not be opened.
Loaded 'ℛ*c9b5c793-2995-47ba-9ae3-b0c8d54c3bdf#1-0'. Cannot find or open the PDB file.
Breakpoint warning: The source code is different from the original version.
To allow this breakpoint to be hit: Add '"requireExactSource": false' to launch.json and restart debugging. - c:\temp\gen\Other.csx:4

I tried setting the requireExactSource to false, but it didn't help.

I can't confirm, but I would guess that your dotnet-script using project.json would have this issue to. Any idea on what would be necessary for that other script to have accurate PDB information loaded as well?

Issues trying to use EF with dotnet-script

I've found an issue trying to run a particular script that uses entity framework with dotnet-script. The script runs fine when executed under csi.exe.

The uncommented #r commands are necessary for this script to run under csi.exe. The ones that are commented out are me responding to the errors i get when trying to run under dotnet-script in vs code.

When trying to get this to work i've seen various errors including:

  • type X is not defined, must reference X.Y
  • not found: '!!0 Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService(System.IServiceProvider)'.
  • Exception thrown: 'System.BadImageFormatException' in System.Private.CoreLib.ni.dll Cannot load a reference assembly for execution.
  • And then messages that would say IQueryable is defined in multiple assemblies
// these dlls are needed to use entity framework
#r "C:\Users\sharpiro\.nuget\packages\microsoft.entityframeworkcore\1.1.2\lib\net451\Microsoft.EntityFrameworkCore.dll"
#r "C:\Users\sharpiro\.nuget\packages\system.interactive.async\3.0.0\lib\net45\System.Interactive.Async.dll"
#r "C:\Users\sharpiro\.nuget\packages\microsoft.extensions.dependencyinjection\1.1.0\lib\netstandard1.1\Microsoft.Extensions.DependencyInjection.dll"
#r "C:\Users\sharpiro\.nuget\packages\microsoft.extensions.dependencyinjection.abstractions\1.1.0\lib\netstandard1.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll"
#r "C:\Users\sharpiro\.nuget\packages\microsoft.extensions.logging.abstractions\1.1.1\lib\netstandard1.1\Microsoft.Extensions.Logging.Abstractions.dll"
#r "C:\Users\sharpiro\.nuget\packages\microsoft.extensions.logging\1.1.1\lib\netstandard1.1\Microsoft.Extensions.Logging.dll"
#r "C:\Users\sharpiro\.nuget\packages\microsoft.extensions.caching.memory\1.1.1\lib\net451\Microsoft.Extensions.Caching.Memory.dll"
#r "C:\Users\sharpiro\.nuget\packages\remotion.linq\2.1.1\lib\net45\Remotion.Linq.dll"
#r "C:\Users\sharpiro\.nuget\packages\microsoft.extensions.options\1.1.1\lib\netstandard1.0\Microsoft.Extensions.Options.dll"
#r "C:\Users\sharpiro\.nuget\packages\microsoft.extensions.caching.abstractions\1.1.1\lib\netstandard1.0\Microsoft.Extensions.Caching.Abstractions.dll"
#r "C:\Users\sharpiro\.nuget\packages\microsoft.entityframeworkcore\1.1.2\lib\net451\Microsoft.EntityFrameworkCore.dll"
#r "C:\Users\sharpiro\.nuget\packages\microsoft.entityframeworkcore.sqlserver\1.1.2\lib\net451\Microsoft.EntityFrameworkCore.SqlServer.dll"
#r "C:\Users\sharpiro\.nuget\packages\microsoft.entityframeworkcore.relational\1.1.2\lib\net451\Microsoft.EntityFrameworkCore.Relational.dll"
#r "C:\Users\sharpiro\.nuget\packages\system.diagnostics.diagnosticsource\4.3.1\lib\net46\System.Diagnostics.DiagnosticSource.dll"
#r "C:\Users\sharpiro\.nuget\packages\microsoft.extensions.primitives\1.1.0\lib\netstandard1.0\Microsoft.Extensions.Primitives.dll"

// trying dlls that c# interactive window uses
// #r "C:\windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll"
// #r "C:\windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll"
// #r "C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll"

// trying dlls that 4.6 uses
// #r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.dll"
// #r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Core.dll"
// #r "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Data.dll"

using Microsoft.EntityFrameworkCore;
public class MyContext : DbContext
{
    public DbSet<Log> Logs { get; set; }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder.UseSqlServer("empty");
}

public class Log
{
    public int Id { get; set; }
    public string Value { get; set; }
}

var context = new MyContext();
var logsQueryable = context.Logs.Take(1);
WriteLine(logsQueryable);

"version": "*" doesn't resolve

The instructions in your README and on your blog say to declare the dependency like so:

"tools": {
    "Dotnet.Script": {
      "version": "*",

However there are only prerelease versions published on NuGet so far.

Dotnet.Script 0.7.0-beta
Dotnet.Script 0.6.0-beta
Dotnet.Script 0.5.0-beta
Dotnet.Script 0.4.0-beta
Dotnet.Script 0.3.1-beta
Dotnet.Script 0.3.0-beta
Dotnet.Script 0.2.0-beta
Dotnet.Script 0.1.1-beta

A * dependency doesn't resolve pre-release versions. This causes an error right off the bat when running dotnet restore.

error: Unable to resolve 'Dotnet.Script' for '.NETCoreApp,Version=v1.0'.

Maybe the instructions should indicate an exact version, a range can cause problems later.

Using the splat string could result in restoring packages from different builds, some of which may be further along than .NET Core 1.0.

Chocolatey Moderation

I got this response from Chocolatey

Requirements represent the minimum quality of a package that is acceptable. When a package version has failed requirements, the package version requires fixing and/or response by the maintainer. Provided a Requirement has flagged correctly, it must be fixed before the package version can be approved. The exact same version should be uploaded during moderation review.
Binary files (.exe, .msi, .zip, etc) have been included without including a LICENSE.txt file. This file is required when including binaries ..
Binary files (.exe, .msi, .zip) have been included without including a VERFICATION.txt file. This file is required when including binaries

I've added the license file to the package, but I have no idea what this verification.txt file is all about. For what should we generate the checksum? dotnet-script.cmd or all the dll's.

Anyhows here is the license fix. I'll just keep this branch until we figure it out
https://github.com/filipw/dotnet-script/tree/bugfix/chocolatey

Support .NET Core 1.1

This needs Roslyn 2.0.0 RC3, Roslyn 2.0.0 RC2 and earlier don't work due to a regression in CoreCLR.

Help with using a netcore2.0 assembly?

First, just wanted to say thank you for making the dotnet-script project. I think it'll really come in handy in the future.

So, I'm trying to use a netcoreapp2.0 assembly (OpenScraping: https://github.com/Microsoft/openscraping-lib-csharp) in a .csx file using dotnet script. But unfortunately, I'm receiving an error:

PS C:\Users\pddomain\CSharpLocalTesting\OpenScrapingProject> dotnet script .\OpenScrapingTest_For_dotnet-script_cli.csx
Script execution resulted in an exception.
Could not load file or assembly 'System.Runtime, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
   at Submission#0.<<Initialize>>d__0.MoveNext()
   at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.Start[TStateMachine](TStateMachine& stateMachine)
   at Submission#0.<Initialize>()
   at Submission#0.<Factory>(Object[] submissionArray)
   at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.<RunSubmissionsAsync>d__9`1.MoveNext()
Could not load the specified file.

So I'm not sure exactly what is causing the issue, but I think that it could be one of the following (although I'm not sure which):

  1. Maybe because dotnet script is a netcoreapp1.1, I can't use netcoreapp2.0 assemblies (not sure if this is a true statement)

  2. Maybe because there's some kind of typename conflict resolution issue, but I'm not sure what specifically would be causing this

  3. Maybe because it insists on using System.Runtime 4.1.1 from my GAC instead of my explicit reference to System.Runtime 4.3.0 in my $HOME\.nuget\packages

  4. Maybe there's some sort of reference syntax mistake I made in my .csx file

Here's all of the info related to my test project:

Folder Structure

OpenScrapingProject
|    ApiPortAnalysis.json
|    Notes.ps1
|    OpenScrapingTest_For_dotnet-script_cli.csx
|    OpenScrapingTest_For_scriptcs_cli.csx
|    OpenScrapingTest.csproj
|    project.json
|    project.lock.json
|    scriptcs_packages.config
|
└───.vscode
|    |   launch.json
└───bin
        └───Debug
            └───netcoreapp2.0
└───obj
    └───Debug
        └───netcoreapp2.0
|    |    |    OpenScrapingTest.AssemblyInfo.cs
|    |    |    ...
|    project.assets.json
|    OpenScrapingProject.csproj.nuget.cache
|    ...
└───scriptcs_packages
    └───HtmlAgilityPack.1.5.1
    └───Newtonsoft.json.10.0.3
    └───OpenScraping.1.0.1
    └───System.Runtime.4.3.0
    └───System.Runtime.Extensions.4.3.0
|   |    ScriptLibraries.csx

A few notes:

  • I have a project.json AND a .csproj so that I can use "nuget restore" or "dotnet restore". Also, it seems like dotnet-script currently needs the project.json (otherwise, I noticed it just creates one on-the-fly in a temp folder, and it insists on targeting netcoreapp1.1 instead of 2.0, which is the main reason why I don't think it is able to handle netcoreapp2.0 assemblies)
  • I have some scriptcs related stuff in there, because I've also been trying to test this with scriptcs (which results in a different error regarding the typename System.AppDomain not being available in System.Runtime.Extensions 4.1.0...which it isn't...it's in mscorlib, but I can't figure out how to force scriptcs to look in mscorlib...)
  • I've installed System.Runtime 4.3.0 and System.Runtime.Extensions 4.3.0 in $HOME\.nuget\packages as well as in my GAC (but it still insists on using 4.1.0 versions of each, which the OpenScraping assembly does not like)

Gist of launch.json: https://gist.github.com/pldmgg/08619a540af6e216d0193d1cca1045ae

Gist of project.json: https://gist.github.com/pldmgg/6d343a25e2bfe5f0b66e04ee060740ab

Gist of OpenScrapingtest.csproj: https://gist.github.com/pldmgg/2c2f65359d0150c71ed81538b1d48ea5

Gist of OpenScrapingTest_For_dotnet-script_cli.csx: https://gist.github.com/pldmgg/89cf205e56975fd2c64af9f6ca22d344

Gist of OpenScrapingTest_For_scriptcs_cli.csx: https://gist.github.com/pldmgg/c118cdbf9226a94faad11fd1583ba6cb

readme.md needs updated

With the latest version, the readme.md is outdated. Need to talk about how to "install" via the zip package (I'd like to see package manager support, such as Chocolatey on Windows and apt-get or other managers on other platforms). Also need to remove the project.json stuff and show the code using the new #r directives.

#r "nuget:AutoMapper,5.1.1"
#r "nuget:Newtonsoft.Json,9.0.1"

using Newtonsoft.Json;
using AutoMapper;

Console.WriteLine("hello!");

var test = new { hi = "i'm json!" };
Console.WriteLine(JsonConvert.SerializeObject(test));

Console.WriteLine(typeof(MapperConfiguration));

Just some feedback... this is a huge step forward, and now it's truly a scripting language. The only downside here is in the performance. The startup time when trying to run the above code is quite significant. Still, this is an awesome way to experiment with C# code.

Allow dependency specification using csproj

Since project.json is effectively a dead technology we should be able to specify dependencies using csproj.

Note this is orthogonal from #58 which only deals with migrating this actual project to csproj, not the dependency specification

release 0.13.0 does not run on Linux Subsystem for Windows

release 0.13.0 does not run on Ubuntu bash for Windows.

Steps to reproduce:

Follow setup guide that verifies the PATH modification in ~/.profile.

Enter this command in bash (Windows): dotnet script
You should get:

No executable found matching command "dotnet-script"
Segmentation fault (core dumped)

Enter this command in bash (Windows): dotnet script.cmd
You should get nothing (but this actually works on full-blown Ubuntu Desktop: dotnet script.sh).

Enter this command in bash (Windows): dotnet exec /mnt/e/your/path/to/dotnet-script/dotne t-script.dll.
You should get dotnet-script command-line args/help.

Enter this command in bash (Windows): dotnet exec /mnt/e/your/path/to/dotnet-script/dotne t-script.dll helloworld.csx.
When you are running the above command from the same folder where helloworld.csx is, you should get: The command dotnet nuget locals global-packages -l failed to execute or it might actually work. Looking into nuget issues on my machine---also there might be issues with dotnet trying to do initial-run metrics setup but the segmentation fault stuff looks bigger than these possibilities.

I am sure with chocolatey and PowerShell it works fine.

Pass the script path to scripts

I was thinking that it would be quite useful to have access to the script path (main script) from within the script itself.
I see that ScriptCs provides this through Env.ScriptPath which is great apart from the fact that OmniSharp does not recognize the Env type and squiggly lines appears in VS Code.

A simple and possible naive solution might be to pass the path of the main script as the first argument to the script. I guess that would be the WorkingDirectory from the ScriptContext class or something like that.

Warnings of downgraded packages during dotnet restore

Clone the repo (26098df at time of reporting), run dotnet restore and one is greeted with 450+ warning about downgrade of packages, e.g.:

warn : Detected package downgrade: System.Text.Encoding from 4.0.11 to 4.0.11-rc3-24128-00
warn :  Dotnet.Script (>= 0.1.1) -> Microsoft.CodeAnalysis.CSharp.Scripting (>= 2.0.0-beta3) -> Microsoft.CodeAnalysis.CSharp (= 2.0.0-beta3) -> Microsoft.CodeAnalysis.Common (= 2.0.0-beta3) -> System.Xml.XPath.XDocument (>= 4.0.1-rc3-24128-00) -> System.Xml.XPath (>= 4.0.1) -> System.IO (>= 4.1.0) -> System.Text.Encoding (>= 4.0.11)

See dotnet-restore.txt for the full log with warnings.

In case it may be of help, my dotnet --info says:

.NET Command Line Tools (1.0.0-preview2-003131)

Product Information:
 Version:            1.0.0-preview2-003131
 Commit SHA-1 hash:  635cf40e58

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.10586
 OS Platform: Windows
 RID:         win10-x64

Nuget assembly reference quirks

It appears assembly referencing is messed up when referencing a nuget package that contains a reference to an existing assembly but located at another path.

This simple csx-script:

#r "nuget:AutoMapper,5.1.1"

using(var filestream = new FileStream("c:\\delete.me", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
}

Gives this compile time error:

error CS1503: Argument 2: cannot convert from 'System.IO.FileMode [c:\Program Files\dotnet\shared\Microsoft.NETCore.App\1.1.2\System.IO.FileSystem.Primitives.dll]' to 'System.IO.FileMode [C:\Users\nicla_000\.nuget\packages\System.IO.FileSystem.Primitives\4.0.1\lib\netstandard1.3\System.IO.FileSystem.Primitives.dll]'

I tried to fix it myself but apparently I don't fully understand how assembly loading works with Roslyn.

No "depsfile" being generated in Nuget package

Hi, just got a .csx breakpoint to hit in VS Code using your tool, very cool stuff!

One issue I had though, was with the "--depsfile", "<NUGET_ROOT>\\Dotnet.Script\\0.7.0-beta\\lib\\netcoreapp1.0\\dotnet-script.deps.json"

When I restore the dotnet-script nuget package, either 0.7.0-beta or 0.9.0-beta, the dotnet-script.deps.json does not exist at the above location where dotnet-script.dll is located, and the debugger would not run without a valid file being provided.

As a workaround I cloned and built locally and pointed to the dotnet-script.deps.jsonfile that gets generated with the build.

Thanks.

Move to csproj?

Did a quick test with reading the runtime dependencies from 'project.assets.json'

Could we do something like this to move to cproj?

As you can see the code is all hacky, but it works in the same way as the ProjecxExporter

PS: No idea what I am doing :)

private IEnumerable<string> GetRuntimeDependenciesFromDepsFile()
{                        
    var runtimeDepedencies = new List<string>();

    var pathToDepsFile = @"C:\Github\scripttest\obj\project.assets.json";
    using (FileStream fs = new FileStream(pathToDepsFile, FileMode.Open, FileAccess.Read))
    {
        using (var reader = new DependencyContextJsonReader())
        {
            var context = reader.Read(fs);
            
            var runtimeLibraries = context.RuntimeLibraries;                    
            foreach (var runtimeLibrary in runtimeLibraries)
            {                       
                if (runtimeLibrary.RuntimeAssemblyGroups.Count > 0)
                {                            
                    var path = runtimeLibrary.Path;
                    foreach (var runtimeLibraryRuntimeAssemblyGroup in runtimeLibrary.RuntimeAssemblyGroups)
                    {
                        if (runtimeLibraryRuntimeAssemblyGroup.Runtime == "win" ||
                            runtimeLibraryRuntimeAssemblyGroup.Runtime == "")
                        {
                            path = Path.Combine(path, runtimeLibraryRuntimeAssemblyGroup.AssetPaths[0]);
                            if (!path.EndsWith("_._"))
                            {
                                runtimeDepedencies.Add(path);
                            }
                            
                        }
                    }
                }
            }

        }
    }
    return runtimeDepedencies;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.