JohnSundell / Files

A nicer way to handle files & folders in Swift
MIT License
2.53k stars 182 forks source link

Command application output wrong. #61

Closed RockerHX closed 4 years ago

RockerHX commented 6 years ago

Code:

import Foundation
import Files

print(Folder.current.path)
print(Folder.home.path)

Xcode run or marathon run output is right.

/Users/xxx/Desktop/AppIcon/
/Users/xxx/

When I use command application: exec, output result is wrong.

/Users/xxx/
/Users/xxx/
clayellis commented 6 years ago

@RockerHX Pardon the potentially obvious question but, are you running exec from your home directory?

RockerHX commented 6 years ago

@clayellis No, I try run it on different folders. I don't know other people have encountered the same problem, or my own system or environment problems?

macOS High Sierra
10.13.4 (17E202)

Apple Swift version 4.1 (swiftlang-902.0.48 clang-902.0.37.1)
Target: x86_64-apple-darwin17.5.0

I try run ./exec in terminal or iTerminal, output is right, but I double click to execute in terminal or iTerminal is wrong. I think double click to execute run in terminal is not a process, and current path is home directory path on terminal.

Can I double click to execute the exec?

Samasaur1 commented 6 years ago

This is correct. When running an executable from the command line, the current directory is the directory the executable is run from. However, if an executable is double-clicked, the current directory is always the home directory.

I don't have an Apple reference handy, but I tested it on my laptop:

Setup for tests:

$ cd Desktop
$ echo "pwd" > workingDir
$ chmod +x workingDir

Run tests from terminal:

$ pwd
/Users/Samasaur1/Desktop
$ cat workingDir
pwd
$ ./workingDir
/Users/Samasaur1/Desktop
$ mkdir newDir
$ cd newDir
$ pwd
/Users/Samasaur1/Desktop/newDir
$ ../workingDir
/Users/Samasaur1/Desktop/newDir
$ cd ../../..
$ pwd
/Users
$ ./Samasaur1/Desktop/workingDir
/Users

Output from double-clicking:

$ /Users/Samasaur1/Desktop/workingDir ; exit;
/Users/Samasaur1
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]

Also, I am aware that Gradle, a Java build tool, says this:

by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong

That's from Gradle-generated executables for Java command-line projects.

In summary, this is Apple's fault (or intent). To avoid this, don't double-click your executable. You could try and use a wrapper script that simply calls your script, or some other method, but Apple has ensured that double-clicking will not give you the output you are expecting. This isn't a problem with Files.