Skip to content

remove hard-coded target directory#3817

Open
dimalinux wants to merge 18 commits intosolana-foundation:masterfrom
dimalinux:workspace-compat
Open

remove hard-coded target directory#3817
dimalinux wants to merge 18 commits intosolana-foundation:masterfrom
dimalinux:workspace-compat

Conversation

@dimalinux
Copy link
Copy Markdown

Anchor is not playing nice with Rust workspaces. If you try to place your Anchor based project as a member in a Rust workspace, the cargo build commands issued by Anchor will place build artifacts in the target/ directory at the top of the Rust workspace, while other Anchor tooling assumes that the target/ directory is located at the top of the "Anchor workspace" (where Anchor.toml is located). The workaround people have been using before this pull request is to add a symbolic link from rust-workspace/anchor-member/target to rust-workspace/target.

Others in the PR issue were having problems because Anchor was not respecting the CARGO_TARGET_DIR environment variable.

This PR fixes both problems by asking cargo (via the cargo metadata command) what directory is currently being used for build artifacts.

Fixes #958

@vercel
Copy link
Copy Markdown

vercel bot commented Aug 3, 2025

@dimalinux is attempting to deploy a commit to the Solana Foundation Team on Vercel.

A member of the Team first needs to authorize it.

@dimalinux dimalinux marked this pull request as ready for review August 5, 2025 21:57
@dimalinux dimalinux force-pushed the workspace-compat branch 2 times, most recently from 00d7a07 to 7351001 Compare August 19, 2025 03:45
Copy link
Copy Markdown
Collaborator

@0x4ka5h 0x4ka5h left a comment

Choose a reason for hiding this comment

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

yea, lgtm now. fixed the issued

@0x4ka5h
Copy link
Copy Markdown
Collaborator

0x4ka5h commented Dec 3, 2025

@dimalinux can you resolve the conflicts so I can run tests

@nutafrost nutafrost added the Merge label Dec 3, 2025
Copilot AI review requested due to automatic review settings December 4, 2025 07:35
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes compatibility issues between Anchor and Rust workspaces by dynamically detecting the cargo target directory instead of using hard-coded paths. The solution uses cargo metadata to query the actual build artifact location, which respects both Rust workspace hierarchies and the CARGO_TARGET_DIR environment variable.

Key Changes:

  • Introduced dynamic target directory resolution via cargo metadata --no-deps --format-version=1
  • Added caching mechanism in Rust using LazyLock to avoid repeated subprocess calls
  • Updated all hard-coded "target" path references across TypeScript and Rust codebases

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
ts/packages/anchor/src/workspace.ts Added execSync call to query cargo metadata for dynamic target directory resolution in workspace proxy
tests/bench/tests/binary-size.ts Updated binary size test to use dynamic target directory from cargo metadata
cli/src/rust_template.rs Updated program keypair path generation to use new target_dir() helper function
cli/src/lib.rs Implemented target_dir() and target_dir_no_cache() functions with LazyLock caching; updated all hard-coded target paths
cli/src/config.rs Replaced hard-coded "target" paths with target_dir() function calls throughout config handling

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@dimalinux
Copy link
Copy Markdown
Author

@0x4ka5h wrote

can you resolve the conflicts so I can run tests

Done. Let me know if there is anything else that I can do.

@jamie-osec jamie-osec added cli and removed Merge labels Feb 27, 2026
Comment on lines +4890 to +4908
fn target_dir_no_cache() -> Result<PathBuf> {
// `cargo metadata` produces a JSON blob from which we extract the
// `target_directory` field.
let output = std::process::Command::new("cargo")
.args(["metadata", "--no-deps", "--format-version=1"])
.output()
.context("Failed to execute 'cargo metadata'")?;

if !output.status.success() {
let stderr_msg = String::from_utf8_lossy(&output.stderr);
if stderr_msg.contains("Cargo.toml") {
// `anchor init` starts populating the cargo artifacts dir
// before creating `Cargo.toml`, in which case "target" in
// the current dir is the desired behavior.
return Ok(PathBuf::from("target"));
}
eprintln!("'cargo metadata' failed with: {stderr_msg}");
std::process::exit(output.status.code().unwrap_or(1));
}
Copy link
Copy Markdown
Collaborator

@jamie-osec jamie-osec Feb 27, 2026

Choose a reason for hiding this comment

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

On a second review, I think this is a confusing design with 4 distinct results

  • If the cargo execution fails, we return an error
  • If cargo execution succeeds, but the status is not success
    • We use a heuristic to special case the error from the anchor init case
    • Otherwise, we print an error and hard exit immediately
  • Otherwise, the output is parsed and used

I would suggest

  • Use Path::from("target") directly as previously in anchor init as we know special handling is required, so calling this is pointless
  • Either always exit, or always return an error from this function
    • I would probably suggest the former, as there's not much that can be done to handle this error, and it simplifies the API for callers

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@jamie-osec: I implemented both suggestions.

The anchor init handling to simplify the responsibilities of target_dir() is in this commit: dimalinux@27d6739

Having target_dir() exit the process in all failure scenarios is in this commit:
dimalinux@3bc3a0e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anchor deploy fails when CARGO_TARGET_DIR is set.

5 participants