0

I tried to run the csharp code in Visual Studio Code for Mac, I have scripts installed and added to PATH.
It gives me below error and code cannot run.

[Running] scriptcs "/var/folders/sr/5jzw91rn4g3cc2b0xyhw0j100000gq/T/tempCodeRunnerFile.csharp"
    Unexpected named argument: var/folders/sr/5jzw91rn4g3cc2b0xyhw0j100000gq/T/tempCodeRunnerFile.csharp

This is the question linked: Getting error: /bin/sh scriptcs: command not found

The OS version is 10.14.6(18G87) Please see attached screenshot. Mac OS version

The Visual Studio Code version is below:

Version: 1.38.1
Commit: b37e54c98e1a74ba89e03073e5a3761284e3ffb0
Date: 2019-09-11T13:31:32.854Z
Electron: 4.2.10
Chrome: 69.0.3497.128
Node.js: 10.11.0
V8: 6.9.427.31-electron.0
OS: Darwin x64 18.7.0

minimal reproducible example:

Open Visual Studio Code and create a new File with below code:

using System;
using System.Collections.Generic;
class Test
{
    static void Main()
    {
        foreach (int fib in Fibs(6))
        Console.Write (fib + " ");
    }
    static IEnumerable<int> Fibs (int fibCount)
    {
        for (int i = 0, prevFib = 1, curFib = 1; i < fibCount; i++)
        {
             yield return prevFib;
             int newFib = prevFib+curFib;
             prevFib = curFib;
             curFib = newFib;
        }
     }
 }

and then click Run Code on top right in Visual Studio Code.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Huibin Zhang
  • 1,072
  • 1
  • 15
  • 30

1 Answers1

2

This is a bug in PowerArgs, it recognizes an argument started with slash as an option, so you need split the command into two steps.

cd "/var/folders/sr/5jzw91rn4g3cc2b0xyhw0j100000gq/T"
scriptcs "tempCodeRunnerFile.csharp"

[Update] scriptcs is a csharp code runner, to make it work, at least you need one line to call the entry method.

...

class Test
{
    public static void Main() // Public
    {
        ...
    }
}

Test.Main(); // Run it
shingo
  • 18,436
  • 5
  • 23
  • 42
  • I tried that in terminal of visual stuido code, but it doesn't give any output.C02RW5PQG8WN:firstcsharpProj i333417$ cd /Users/i333417/Downloads/firstcsharpProj/ C02RW5PQG8WN:firstcsharpProj i333417$ scriptcs Program.cs C02RW5PQG8WN:firstcsharpProj i333417$ scriptcs Program.cs C02RW5PQG8WN:firstcsharpProj i333417$ cd /Users/i333417/Downloads/firstcsharpProj/ C02RW5PQG8WN:firstcsharpProj i333417$ scriptcs Program.cs – Huibin Zhang Sep 19 '19 at 08:03
  • 1
    OK, if the example in the post is Program.cs then I think you pervert scriptcs, see my update. – shingo Sep 19 '19 at 08:29
  • thanks that works now, just two minor things: in the code I added Test.Main(); but it gives me some message: A namespace cannot directly contain members such as fields or methods (CS0116) [firstcsharpProj] Peek Problem No quick fixes available – Huibin Zhang Sep 19 '19 at 08:40
  • and each time I press run button it automatically run scriptcs @shingo @ "/Users/i333417/Downloads/firstcsharpProj/Program.cs" that gives error, could I change the default behavior so that when press the run button it will enter the fold first and then run the scriptcs Program.cs, so that i don't have to manually type the 2 commands in the terminal to run code. Thanks – Huibin Zhang Sep 19 '19 at 08:42
  • 1
    1. namespace in scripts is not supported by scriptcs. 2. you can write a shell to run these two commands (`cd ...` and `scriptcs ..`), and change scriptcs path to this shell (Preferences > UserSettings > Extensions > ScriptCsRunner configuration > Scriptcs Path). – shingo Sep 19 '19 at 09:23
  • would it be possible to let the shell script to pick up the file path and file name to run automatically, otherwise that shell scripts need to be manually amended each time a new file is created or file path is changed? Thanks. – Huibin Zhang Sep 19 '19 at 09:57
  • something like cd $path_to_current_file scriptcs $current_file_name – Huibin Zhang Sep 19 '19 at 09:58
  • 1
    The script is passed as the third argument, so something like `${2##*/}` for file name and `${2%/*}` for folder path. these syntax works in linux shell, not sure do they work in mac too. – shingo Sep 19 '19 at 14:04