-
Notifications
You must be signed in to change notification settings - Fork 1.9k
remove hard-coded target directory
#3817
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
base: master
Are you sure you want to change the base?
Changes from 13 commits
24ecdea
b7a9499
07f89e3
0f89f7a
264598f
666d841
9c13cf9
aa68954
f7bf3cc
d631230
11b131f
4613284
4580225
37cdc19
45ed8fa
2feaf99
27d6739
3bc3a0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ use heck::{ToKebabCase, ToLowerCamelCase, ToPascalCase, ToSnakeCase}; | |
| use regex::{Regex, RegexBuilder}; | ||
| use rust_template::{ProgramTemplate, TestTemplate}; | ||
| use semver::{Version, VersionReq}; | ||
| use serde::Deserialize; | ||
| use serde_json::{json, Map, Value as JsonValue}; | ||
| use solana_cli_config::Config as SolanaCliConfig; | ||
| use solana_commitment_config::CommitmentConfig; | ||
|
|
@@ -1731,13 +1732,13 @@ pub fn build( | |
|
|
||
| let idl_out = match idl { | ||
| Some(idl) => Some(PathBuf::from(idl)), | ||
| None => Some(cfg_parent.join("target").join("idl")), | ||
| None => Some(target_dir()?.join("idl")), | ||
| }; | ||
| fs::create_dir_all(idl_out.as_ref().unwrap())?; | ||
|
|
||
| let idl_ts_out = match idl_ts { | ||
| Some(idl_ts) => Some(PathBuf::from(idl_ts)), | ||
| None => Some(cfg_parent.join("target").join("types")), | ||
| None => Some(target_dir()?.join("types")), | ||
| }; | ||
| fs::create_dir_all(idl_ts_out.as_ref().unwrap())?; | ||
|
|
||
|
|
@@ -1912,7 +1913,7 @@ fn build_cwd_verifiable( | |
| ) -> Result<()> { | ||
| // Create output dirs. | ||
| let workspace_dir = cfg.path().parent().unwrap().canonicalize()?; | ||
| let target_dir = workspace_dir.join("target"); | ||
| let target_dir = target_dir()?; | ||
| fs::create_dir_all(target_dir.join("verifiable"))?; | ||
| fs::create_dir_all(target_dir.join("idl"))?; | ||
| fs::create_dir_all(target_dir.join("types"))?; | ||
|
|
@@ -1945,17 +1946,15 @@ fn build_cwd_verifiable( | |
| let idl = generate_idl(cfg, skip_lint, no_docs, &cargo_args)?; | ||
| // Write out the JSON file. | ||
| println!("Writing the IDL file"); | ||
| let out_file = workspace_dir | ||
| .join("target") | ||
| let out_file = target_dir | ||
| .join("idl") | ||
| .join(&idl.metadata.name) | ||
| .with_extension("json"); | ||
| write_idl(&idl, OutFile::File(out_file))?; | ||
|
|
||
| // Write out the TypeScript type. | ||
| println!("Writing the .ts file"); | ||
| let ts_file = workspace_dir | ||
| .join("target") | ||
| let ts_file = target_dir | ||
| .join("types") | ||
| .join(&idl.metadata.name) | ||
| .with_extension("ts"); | ||
|
|
@@ -3436,7 +3435,7 @@ fn validator_flags( | |
| idl.address = address; | ||
|
|
||
| // Persist it. | ||
| let idl_out = Path::new("target") | ||
| let idl_out = target_dir()? | ||
| .join("idl") | ||
| .join(&idl.metadata.name) | ||
| .with_extension("json"); | ||
|
|
@@ -3655,7 +3654,7 @@ fn stream_logs(config: &WithPath<Config>, rpc_url: &str) -> Result<Vec<LogStream | |
|
|
||
| // Subscribe to logs for all workspace programs | ||
| for program in config.read_all_programs()? { | ||
| let idl_path = Path::new("target") | ||
| let idl_path = target_dir()? | ||
| .join("idl") | ||
| .join(&program.lib_name) | ||
| .with_extension("json"); | ||
|
|
@@ -3890,7 +3889,7 @@ fn clean(cfg_override: &ConfigOverride) -> Result<()> { | |
| }; | ||
|
|
||
| let dot_anchor_dir = workspace_root.join(".anchor"); | ||
| let target_dir = workspace_root.join("target"); | ||
| let target_dir = crate::target_dir()?; | ||
| let deploy_dir = target_dir.join("deploy"); | ||
|
|
||
| if dot_anchor_dir.exists() { | ||
|
|
@@ -4586,9 +4585,51 @@ fn localnet( | |
| })? | ||
| } | ||
|
|
||
| /// Return the directory where cargo is storing build artifacts. Caches the | ||
| /// result assuming that a single run will only work with a single rust | ||
| /// workspace. | ||
| pub fn target_dir() -> Result<PathBuf> { | ||
| static TARGET_DIR: LazyLock<Result<PathBuf>> = LazyLock::new(target_dir_no_cache); | ||
| match &*TARGET_DIR { | ||
| Ok(path) => Ok(path.clone()), | ||
| Err(e) => Err(anyhow::anyhow!(e.to_string())), | ||
| } | ||
| } | ||
|
|
||
| /// Return the directory where cargo is storing build artifacts. | ||
| 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)); | ||
dimalinux marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
Comment on lines
+4799
to
+4810
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
I would suggest
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jamie-osec: I implemented both suggestions. The Having |
||
|
|
||
| #[derive(Deserialize)] | ||
| struct CargoMetadata { | ||
| target_directory: PathBuf, | ||
| } | ||
|
|
||
| let metadata: CargoMetadata = serde_json::from_slice(&output.stdout)?; | ||
|
|
||
| Ok(metadata.target_directory) | ||
| } | ||
|
|
||
| // with_workspace ensures the current working directory is always the top level | ||
| // workspace directory, i.e., where the `Anchor.toml` file is located, before | ||
| // and after the closure invocation. | ||
| // where the `Anchor.toml` file is located, before and after the closure | ||
| // invocation. | ||
| // | ||
| // The closure passed into this function must never change the working directory | ||
| // to be outside the workspace. Doing so will have undefined behavior. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.