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

Obtaining the path to a script and/or its containing directory through a symlink #121

Open
kellytk opened this issue Nov 2, 2023 · 0 comments

Comments

@kellytk
Copy link

kellytk commented Nov 2, 2023

#!/usr/bin/env rust-script

// Layout:
// /home/kellytk/symlinkedscript/
// /home/kellytk/symlinkedscript/script/script.rs - this file.
// /home/kellytk/symlinkedscript/symlink/script.rs - symlink (ln -s /home/kellytk/symlinkedscript/script/script.rs /home/kellytk/symlinkedscript/symlink/script.rs)

let path = std::env::var("RUST_SCRIPT_BASE_PATH").expect("fetch failure");

println!(
    "RUST_SCRIPT_BASE_PATH: {:?}",
    path,
);

println!("canonicalized path: {:?}", std::fs::canonicalize(path).expect("canonicalize failure"));

/*
$ pwd
/home/kellytk/symlinkedscript

As expected:
$ ./script/script.rs
RUST_SCRIPT_BASE_PATH: "/home/kellytk/symlinkedscript/./script"
canonicalized path: "/home/kellytk/symlinkedscript/script"

Unexpected:
$ ./symlink/script.rs
RUST_SCRIPT_BASE_PATH: "/home/kellytk/symlinkedscript/./symlink"
canonicalized path: "/home/kellytk/symlinkedscript/symlink"

Expected:
$ ./symlink/script.rs
RUST_SCRIPT_BASE_PATH: "/home/kellytk/symlinkedscript/./script"
canonicalized path: "/home/kellytk/symlinkedscript/script"
*/

How please can the expected result be obtained in both cases?

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

1 participant