swiftlang / swift-package-manager

The Package Manager for the Swift Programming Language
Apache License 2.0
9.71k stars 1.33k forks source link

[SR-15097] [swift-tools-support-core] TSCBasic.Process does not wait for readLine input. #4391

Open swift-ci opened 3 years ago

swift-ci commented 3 years ago
Previous ID SR-15097
Radar rdar://problem/82178262
Original Reporter beepluis (JIRA User)
Type Bug
Environment ``` java > xcodebuild -version Xcode 12.5.1 Build version 12E507 > swift --version Apple Swift version 5.4.2 (swiftlang-1205.0.28.2 clang-1205.0.19.57) Target: x86_64-apple-darwin20.6.0 ```
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Package Manager | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: 2402872cb77832cf2be5b6a466e5f1ea

Issue Description:

This is a bug report for project: https://github.com/apple/swift-tools-support-core

Summary:

When using `TSCBasic.Process` to run an external executable that uses `readLine` to read user input, the process does not wait for input and instead exits causing issues.

Description:

We are using `TSBasic.Process` to run external executables like the one in question (https://github.com/RobotsAndPencils/xcodes)) this particular CLI prompts users to enter an ID when running a command, using `readLine` (see here). It looks like `TSCBasic.Process` is not waiting for user input an as such the program exits with an error, similar to:

Apple ID: The operation couldn't be completed. (ExitCode(rawValue: 1))

I verified this seems isolated to `TSCBasic.Process` by running the same executable with `Foundation.Process`. The foundation process version in fact waits for user input and the application works as intended.

Im not exactly sure what could be happening here to cause this issue but judging by how many applications use `readLine` to process input this is bug that is blocking us from continuing work.

typesanitizer commented 3 years ago

@swift-ci create

abertelrud commented 3 years ago

I would expect that if a stdin is passed (even empty), then it would be written and the input pipe closed and you'd see what you see here. But if no stdin is passed (the default), then stdin would be kept open and connected to whatever pty the parent had. So this sounds like a problem in TSCBasic.Process.

I supposed the problem is that 9 times out to 10, if you're invoking some tool like a compiler or linker or whatnot or even Git, you really don't want it to wait forever on input but rather to fail, so I would expect that usually you'd want to pass an empty stdin. So maybe this should be a more specific option than whether or not to pass stdin.

Either way, I think this type predates Foundation, and in general we probably want to move away from TSCBasic.Process in favor of Foundation.Process.

swift-ci commented 3 years ago

Comment by Luis Padron (JIRA)

@abertelrud Thank you for your speedy reply here!

We aren't using stdin at all in the app so I think your assessment here is correct that this is some issue in TSBasic.Process.

We'll take your advice and look to migrate from TSCBasic to Foundation for this work. Just wanted to open this to ensure it gets tracked somewhere for any future users.

Thanks again for your insight!

swift-ci commented 3 years ago

Comment by Luis Padron (JIRA)

@abertelrud Following back up here, while migrating to `Foundation.Process` I seem to have hit a roadblock in that handling interactive programs with `Process` seems to be more complicated than I previously imagined.

I'm not sure the best avenue for getting these questions answered but since I'm unfortunately stuck (and have been since I reported this issue) I'm not sure where else to go. The existing documentation for this hasn't helped me solve the problem, so I'm at this point where I can continue using the `TSCBasic.Process` and look for a bug fix to the above issue or get help with migrating to `Foundation.Process`. I'm looking for help running arbitrary executables using `Foundation.Process`. I posted in the Apple Developer Forums HERE. I'd really appreciate some help here if you or someone with some experience could help out!