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

feat(query): reverse tracing #9319

Merged
merged 11 commits into from
Oct 29, 2024
Merged

Conversation

NicholasLYang
Copy link
Contributor

@NicholasLYang NicholasLYang commented Oct 23, 2024

Description

Implements an MVP of reverse tracing. Basically goes through all the JavaScript/TypeScript files in the repo, checks which ones import the reverse traced file, and returns those. Does this concurrently with a JoinSet.

Also adds code to detect if a tsconfig is in scope for a file and if so, add it to the resolver. That way custom aliases work.

Testing Instructions

Added some tests including a monorepo setup with tsconfigs in the packages that define aliases

Copy link

vercel bot commented Oct 23, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
examples-nonmonorepo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 29, 2024 3:21pm
8 Skipped Deployments
Name Status Preview Comments Updated (UTC)
examples-basic-web ⬜️ Ignored (Inspect) Visit Preview Oct 29, 2024 3:21pm
examples-designsystem-docs ⬜️ Ignored (Inspect) Visit Preview Oct 29, 2024 3:21pm
examples-gatsby-web ⬜️ Ignored (Inspect) Visit Preview Oct 29, 2024 3:21pm
examples-kitchensink-blog ⬜️ Ignored (Inspect) Visit Preview Oct 29, 2024 3:21pm
examples-native-web ⬜️ Ignored (Inspect) Visit Preview Oct 29, 2024 3:21pm
examples-svelte-web ⬜️ Ignored (Inspect) Visit Preview Oct 29, 2024 3:21pm
examples-tailwind-web ⬜️ Ignored (Inspect) Visit Preview Oct 29, 2024 3:21pm
examples-vite-web ⬜️ Ignored (Inspect) Visit Preview Oct 29, 2024 3:21pm

Copy link

socket-security bot commented Oct 25, 2024

New dependencies detected. Learn more about Socket for GitHub ↗︎

Package New capabilities Transitives Size Publisher
npm/[email protected] Transitive: filesystem +25 234 kB jonschlinkert

View full report↗︎


let mut parser = Parser::new_from(Capturing::new(lexer));
pub async fn reverse_trace(mut self) -> TraceResult {
let files = match globwalk::globwalk(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love at some point to combine the glob walking and the import resolution so that we can have more concurrency. Right now we have to walk the entire repo, then do the import resolution step.

Copy link
Member

@chris-olszewski chris-olszewski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Before we do any perf tinkering we should collect some profiles or set up a benchmark.

for error in &result.errors {
eprintln!("error: {}", error);
for error in result.errors {
println!("{:?}", Report::new(error))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we're printing errors to stdout?

Suggested change
println!("{:?}", Report::new(error))
eprintln!("{:?}", Report::new(error))

@@ -12,12 +13,17 @@ struct Args {
cwd: Option<Utf8PathBuf>,
#[clap(long)]
ts_config: Option<Utf8PathBuf>,
#[clap(long)]
node_modules: Option<Utf8PathBuf>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be any node_modules or should it be the most specific one available?
e.g. repo/node_modules or repo/packages/foo/node_modules?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Y'know I'm not exactly sure. oxc_resolver is a little ambiguous here. This CLI isn't meant for public use, mostly just for testing

Comment on lines 1150 to 1151
#[cfg(windows)]
let repo_root = repo_root.to_realpath()?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love a comment explaining why this is necessary only on Windows.

Also, we should avoid conditional compilation except where it's necessary.

Suggested change
#[cfg(windows)]
let repo_root = repo_root.to_realpath()?;
let repo_root = if cfg!(windows) {
repo_root.to_realpath()?
} else {
repo_root
};

file_path: &AbsoluteSystemPath,
) -> Option<(Vec<AbsoluteSystemPathBuf>, SeenFile)> {
// Read the file content
let Ok(file_content) = tokio::fs::read_to_string(&file_path).await else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not worth blocking, but I wonder if throwing rayon or a thread pool would be more effective for the reverse trace. The only concurrency we're getting is the file reads and I'm not sure how much of the weight of this function is FS vs parsing and extracting info.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I'm not exactly sure. I think we should do some investigation after it gets some use


// Parse the file as a module
let Ok(module) = parser.parse_module() else {
errors.push(TraceError::FileNotFound(file_path.to_owned()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be a TraceError::ParseError?

}
}

pub fn trace(mut self, max_depth: Option<usize>) -> TraceResult {
pub async fn get_imports_from_file(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For any future perf work it might be helpful to add some spans in this function so we can tell where time is being spent.

@NicholasLYang NicholasLYang merged commit 10f294b into main Oct 29, 2024
40 checks passed
@NicholasLYang NicholasLYang deleted the nicholasyang/reverse-turbo-trace branch October 29, 2024 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants