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

[Proposal]: Add #! as a preprocessor directive equivalent to a single line comment #4281

Closed
1 of 4 tasks
i-am-shodan opened this issue Dec 28, 2020 · 2 comments
Closed
1 of 4 tasks

Comments

@i-am-shodan
Copy link

FEATURE_NAME

  • Proposed
  • Prototype: Not Started
  • Implementation: Not Started
  • Specification: Not Started

Summary

Unix environments are able to execute many kinds of scripted programs directly via the '#!' magic header at the start of a plain text file. These bytes combined with execute permissions on the file cause the OS to look up and execute a separate program which is passed the script file as an argument - this results in execution of the script. This is used frequently with BASH shell scripts but also Python as well.

In many languages (Python, Bash etc) this functionality is enabled by treating the # character as a comment. C# does not treat # as a comment and so it is not currently possible to compile and execute a script directly as per the example below.

wget https://www.contoso.com/someprogram.cs
chmod +x someprogram.cs
./someprogram.cs

Motivation

  • Aid Unix adoption by enabling drop in replacement for BASH/Python shell scripts

Detailed design

The pre-processor already supports a range of # directives C# Pre-processor directives. I suggest a new pre-processor directive '#!' be added that would function as a single line comment.

The pre-processor implementation would simply replace #! with //. It would also be recommended to make this directive only valid at the start of the file thereby mitigating the risk of breaking changes. As #! is not currently a preprocessor directive the risk of breaking changes is already minimal.

This proposal purposely doesn't define how the C# file will be compiled or run, this could either be:

  • Supplied as part of the runtime
  • Another script which performs the build and run
  • Something specified by the user

The snippets below give example syntax before and after the pre-processor has run

#!/usr/bin/env someunspecifiedprogram
using System;
 
namespace HelloWorld
{
    class Hello {         
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}
///usr/bin/env someunspecifiedprogram
using System;
 
namespace HelloWorld
{
    class Hello {         
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Code changes required by accepting this feature as not particularly invasive as it is a similar case to the existing #define syntax (search SyntaxKind.DefineDirectiveTrivia for files that would likely need to be changed)

Drawbacks

As with any language syntax change this would potentially result in syntax errors in tools that were not #! aware. It is also not particularly useful to those running Windows environments.

Alternatives

None, #! must be the first two bytes of the file

Unresolved questions

Design meetings

@yaakov-h
Copy link
Member

Duplicate of #3507.

@333fred
Copy link
Member

333fred commented Dec 29, 2020

Closing as a duplicate.

@333fred 333fred closed this as completed Dec 29, 2020
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

No branches or pull requests

3 participants