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

[SR-9247] Hashbang Swift scripts do not have a proper commandline #3592

Open
swift-ci opened this issue Nov 14, 2018 · 4 comments
Open

[SR-9247] Hashbang Swift scripts do not have a proper commandline #3592

swift-ci opened this issue Nov 14, 2018 · 4 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-9247
Radar None
Original Reporter helge (JIRA User)
Type Bug
Environment

Swift 4.2.1, macOS 10.13.6, MacPro (pre-RISC-V)

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: 467fb15faf12b03bf28726c514830e2e

Issue Description:

Given a script like this `cmdline.swift`:

#!/usr/bin/swift


import Foundation


let pi = ProcessInfo.processInfo
print(pi.arguments) 

You would expect an argument array like this:

./cmdline.swift a 10
[ "./cmdline.swift", "a", "10" ]

Instead we get this beauty:

["/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift", "-frontend", "-interpret", "./cmdline.swift", "-enable-objc-interop", "-sdk", "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk", "-color-diagnostics", "-module-name", "cmdline"] 

The frontend needs to patch the cmdline properly when it is being run as an interpreter.

@belkadan
Copy link

We've looked into this before and it's rather tricky on Darwin because Foundation is looking directly at the process argv pointer. You can use CommandLine.arguments instead.

@belkadan
Copy link

The Radar I can find for this (rdar://problem/27034223) was closed as NTBF, since ProcessInfo.arguments is a pretty low-level access to command-line arguments. I suppose we could mark that as deprecated, though? (if we had a way to mark something as deprecated in Swift but not ObjC)

cc @millenomi

@swift-ci
Copy link
Contributor Author

Comment by Helge Heß (JIRA)

I don’t know for sure, but isn’t this what other interpreters do, directly patch the argv pointer with a replacement array?
The thing I can imagine is that NSProcessInfo caches, and therefore can’t pickup changes /shrug

@belkadan
Copy link

PyObjC doesn't, for example:

#!/usr/bin/python
import Foundation
print(Foundation.NSProcessInfo.processInfo().arguments())

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants