Skip to content

Commit

Permalink
If you pass ISO_PRINT_ABSOLUTE_FILEPATH=1 before running the isograph…
Browse files Browse the repository at this point in the history
… compiler, it will

print absolute filepaths.

Reproducing the comment from the source code:

        // HACK
        //
        // When we run pnpm build-pet-demo (etc), then the terminal's working directory is
        // the isograph folder. But the process thinks that the working directory is
        // /demos/pet-demo. As a result, if we print relative paths, we can't command-click
        // on them, leading to a worse developer experience when working on Isograph.
        //
        // On the other hand, printing relative paths (from the current working directory):
        // - is a nice default
        // - means that if we capture that output, e.g. for fixtures, we can have consistent
        //   fixture output, no matter what machine the fixtures were generated on.
        //
        // So, we need both options. This can probably be improved somewhat.

I am not happy with this situation! But Claude says there's no reliable cross-platform way to read the parent process's working directory.
  • Loading branch information
rbalicki2 committed Jan 6, 2025
1 parent a421679 commit 6145e31
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
38 changes: 31 additions & 7 deletions crates/common_lang_types/src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub struct TextSource {
pub span: Option<Span>,
}

const ISO_PRINT_ABSOLUTE_FILEPATH: &str = "ISO_PRINT_ABSOLUTE_FILEPATH";

impl TextSource {
pub fn read_to_string(&self) -> (String, String) {
// TODO maybe intern these or somehow avoid reading a bajillion times.
Expand All @@ -29,16 +31,38 @@ impl TextSource {
let relative_path = self.relative_path_to_source_file.lookup();
file_path.push(relative_path);

let file_contents = std::fs::read_to_string(&file_path).expect("file should exist");
let file_path = file_path
.to_str()
.expect("Expected path to be able to be stringifie.")
.to_string();
// HACK
//
// When we run pnpm build-pet-demo (etc), then the terminal's working directory is
// the isograph folder. But the process thinks that the working directory is
// /demos/pet-demo. As a result, if we print relative paths, we can't command-click
// on them, leading to a worse developer experience when working on Isograph.
//
// On the other hand, printing relative paths (from the current working directory):
// - is a nice default
// - means that if we capture that output, e.g. for fixtures, we can have consistent
// fixture output, no matter what machine the fixtures were generated on.
//
// So, we need both options. This can probably be improved somewhat.
let absolute_or_relative_file_path = if std::env::var(ISO_PRINT_ABSOLUTE_FILEPATH).is_ok() {
file_path
.to_str()
.expect("Expected path to be able to be stringifie.")
.to_string()
} else {
relative_path.to_string()
};

let file_contents =
std::fs::read_to_string(&absolute_or_relative_file_path).expect("file should exist");
if let Some(span) = self.span {
// TODO we're cloning here unnecessarily, I think!
(file_path, file_contents[span.as_usize_range()].to_string())
(
absolute_or_relative_file_path,
file_contents[span.as_usize_range()].to_string(),
)
} else {
(file_path, file_contents)
(absolute_or_relative_file_path, file_contents)
}
}
}
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
"build": "cargo build",
"cross": "cross build",
"build-json-schema": "./target/debug/build_json_schema",
"watch-pet-demo": "pnpm --filter=pet-demo iso-watch",
"watch-github-demo": "pnpm --filter=github-demo iso-watch",
"watch-isograph-react-demo": "pnpm --filter=@isograph/react iso-watch",
"watch-vite-demo": "pnpm --filter=vite-demo iso-watch",
"build-demos": "pnpm -r --parallel --aggregate-output iso",
"build-github-demo": "pnpm --filter=github-demo iso",
"build-pet-demo": "pnpm --filter=pet-demo iso",
"build-isograph-react-demo": "pnpm --filter=@isograph/react iso",
"build-vite-demo": "pnpm --filter=vite-demo iso",
"watch-pet-demo": "ISO_PRINT_ABSOLUTE_FILEPATH=1 pnpm --filter=pet-demo iso-watch",
"watch-github-demo": "ISO_PRINT_ABSOLUTE_FILEPATH=1 pnpm --filter=github-demo iso-watch",
"watch-isograph-react-demo": "ISO_PRINT_ABSOLUTE_FILEPATH=1 pnpm --filter=@isograph/react iso-watch",
"watch-vite-demo": "ISO_PRINT_ABSOLUTE_FILEPATH=1 pnpm --filter=vite-demo iso-watch",
"build-demos": "ISO_PRINT_ABSOLUTE_FILEPATH=1 pnpm -r --parallel --aggregate-output iso",
"build-github-demo": "ISO_PRINT_ABSOLUTE_FILEPATH=1 pnpm --filter=github-demo iso",
"build-pet-demo": "ISO_PRINT_ABSOLUTE_FILEPATH=1 pnpm --filter=pet-demo iso",
"build-isograph-react-demo": "ISO_PRINT_ABSOLUTE_FILEPATH=1 pnpm --filter=@isograph/react iso",
"build-vite-demo": "ISO_PRINT_ABSOLUTE_FILEPATH=1 pnpm --filter=vite-demo iso",
"watch-libs": "bacon -j watch-libs -p ./libs",
"format": "pnpm run format-prettier && pnpm run format-rust",
"format-prettier": "prettier --config ./.prettierrc.json --write .",
Expand Down

0 comments on commit 6145e31

Please sign in to comment.