Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for CSharp Script #1141

Closed
Eptagone opened this issue Jan 23, 2024 · 3 comments · Fixed by #1142 or #1151
Closed

Support for CSharp Script #1141

Eptagone opened this issue Jan 23, 2024 · 3 comments · Fixed by #1142 or #1151
Milestone

Comments

@Eptagone
Copy link

Hi guys. I'm having some issues trying to format my C# scripts. 
C# can be used for scripting. There are tools like dotnet-script to run those files. C# scripts use the .csx extension but csharper doesn't support it. Is there a way to force csharper to format those files?

Input:
dotnet-csharpier ./main.csx

Output:
Warning ./main.csx - Is an unsupported file type.

Expected behavior:
The file formatted.

@belav
Copy link
Owner

belav commented Jan 23, 2024

There isn't currently a way to tell CSharpier which files to format and with which formatter, but I do have a branch where I have csproj/xml formatting mostly done and will need to figure it out.

For now though it will be easy to change the hardcoded ".cs" checks in the cli to ".cs/.csx".

Changing the extensions should also happen, but is probably less important.

@belav belav added this to the 0.27.1 milestone Jan 23, 2024
@Eptagone
Copy link
Author

Eptagone commented Jan 24, 2024

Hi, thanks @belav but now there is another problem, can this issue be open again or should I open a new issue?

If I try to run Csharpier in my file, the following error appears:

❯  dotnet csharpier ./main.csx
Warning ./main.csx - Failed to compile so was not formatted.
  (1,1): error CS1024: Preprocessor directive expected
  (3,2): error CS8097: #load is only allowed in scripts
Formatted 1 files in 266ms.

As I can see, CSharpier cannot process files with the #!/usr/bin/env dotnet-script and the #load directive.
This is an example file:

#!/usr/bin/env dotnet-script
#load "other.csx"

WriteLine("Hello World");

If I remove those lines, CSharpier works as expected.
There are no errors with those lines. #!/usr/bin/env dotnet-script is required so I can run the script directly and the #load directive is used to load additional scripts.

UPDATE
Looks like using any directive doesn't work. #r directive also fails:

// Example
#r "nuget: AngleSharp, 1.1.0"
❯  dotnet csharpier ./scrapper.csx
Warning ./scrapper.csx - Failed to compile so was not formatted.
  (2,2): error CS7011: #r is only allowed in scripts
  (3,2): error CS7011: #r is only allowed in scripts

@belav
Copy link
Owner

belav commented Jan 25, 2024

That's my bad, I assumed csx were just regular c# code and only tested that way. From my googling it looks like Roslyn can still parse it into a syntax tree, so it should be straightforward for me to get working, it just won't be the two line change that I hoped.

@belav belav reopened this Jan 25, 2024
@belav belav modified the milestones: 0.27.1, 0.27.2 Jan 26, 2024
belav added a commit that referenced this issue Jan 26, 2024
belav added a commit that referenced this issue Jan 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants