Skip to content

Commit fee554a

Browse files
authored
Fix: Set working directory for cargo metadata and cargo clean to ensure config discovery (#646)
* tests: Add `cargo_config_discovery` test * fix: set working directory for cargo to ensure config discovery Set `WORKING_DIRECTORY` for all `cargo` invocations to the manifest's directory, matching the existing behavior for `cargo build`. This ensures that Cargo properly discovers configuration files like `.cargo/config.toml` and `toolchain.toml` by walking upward from the manifest directory, rather than from CMake's current working directory. Updated comments to clarify that Cargo searches for configuration files by walking upward from the current directory. * test fix: build executable for `cargo_config_discovery_run_cargo_config_discovery`. * test fix: adjust registry alias / dependency for older toolchain * test fix: ensure `cargo-clean` waits until `*_run_${exe}` completes. * test fix: shorten name to `config_discovery` to avoid Windows path limit
1 parent 614508f commit fee554a

File tree

10 files changed

+88
-5
lines changed

10 files changed

+88
-5
lines changed

cmake/Corrosion.cmake

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -898,8 +898,10 @@ function(_add_cargo_build out_cargo_build_out_dir)
898898
# to determine the correct target directory, depending on if the hostbuild target property is
899899
# set or not.
900900
# BYPRODUCTS "${cargo_build_dir}/${build_byproducts}"
901-
# The build is conducted in the directory of the Manifest, so that configuration files such as
902-
# `.cargo/config.toml` or `toolchain.toml` are applied as expected.
901+
902+
# Set WORKING_DIRECTORY to the directory containing the manifest, so that configuration files
903+
# such as `.cargo/config.toml` or `toolchain.toml` are applied as expected. Cargo searches for
904+
# configuration files by walking upward from the current directory.
903905
WORKING_DIRECTORY "${workspace_toml_dir}"
904906
${cor_uses_terminal}
905907
COMMAND_EXPAND_LISTS
@@ -930,7 +932,10 @@ function(_add_cargo_build out_cargo_build_out_dir)
930932
COMMAND
931933
"${cargo_bin}" clean ${cargo_target_option}
932934
-p ${package_name} --manifest-path "${path_to_toml}"
933-
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/${build_dir}"
935+
# Set WORKING_DIRECTORY to the directory containing the manifest, so that configuration files
936+
# such as `.cargo/config.toml` or `toolchain.toml` are applied as expected. Cargo searches for
937+
# configuration files by walking upward from the current directory.
938+
WORKING_DIRECTORY "${workspace_toml_dir}"
934939
${cor_uses_terminal}
935940
)
936941

@@ -2378,4 +2383,3 @@ macro(_corrosion_arg_passthrough_helper arg_name prefix var_name)
23782383
endmacro()
23792384

23802385
list(POP_BACK CMAKE_MESSAGE_CONTEXT)
2381-

cmake/CorrosionGenerator.cmake

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ function(_cargo_metadata out manifest)
2121
if(FROZEN)
2222
set(cargo_frozen "--frozen")
2323
endif()
24+
25+
get_filename_component(workspace_toml_dir "${manifest}" DIRECTORY)
26+
2427
execute_process(
2528
COMMAND
2629
${CMAKE_COMMAND} -E env
@@ -33,7 +36,10 @@ function(_cargo_metadata out manifest)
3336
--no-deps
3437
${cargo_locked}
3538
${cargo_frozen}
36-
39+
# Set WORKING_DIRECTORY to the directory containing the manifest, so that configuration files
40+
# such as `.cargo/config.toml` or `toolchain.toml` are applied as expected. Cargo searches for
41+
# configuration files by walking upward from the current directory.
42+
WORKING_DIRECTORY "${workspace_toml_dir}"
3743
OUTPUT_VARIABLE json
3844
COMMAND_ERROR_IS_FATAL ANY
3945
)

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ function(corrosion_tests_add_test test_name bin_names)
163163
endfunction()
164164

165165
# Please keep this in alphabetical order.
166+
add_subdirectory(config_discovery)
166167
add_subdirectory(cargo_flags)
167168
add_subdirectory(cpp2rust)
168169
if(Rust_VERSION VERSION_GREATER_EQUAL "1.64.0")
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# The `.cargo/config.toml` file registers the `my-registry` index. If Cargo fails to
2+
# discover the config file, the build will fail with:
3+
# "registry index was not found in any configuration: `my-registry`"
4+
corrosion_tests_add_test(config_discovery "config_discovery")
5+
6+
# Run the `cargo-clean` target to verify `.cargo/config.toml` discovery.
7+
add_test(NAME "config_discovery_run_cargo_clean"
8+
COMMAND "${CMAKE_COMMAND}" --build "${CMAKE_CURRENT_BINARY_DIR}/build-config_discovery" --target cargo-clean
9+
)
10+
set_tests_properties("config_discovery_run_cargo_clean" PROPERTIES
11+
FIXTURES_REQUIRED "build_fixture_config_discovery"
12+
DEPENDS "config_discovery_run_config_discovery"
13+
)

test/config_discovery/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Cargo Configuration Discovery Test
2+
3+
This test verifies that `cargo` correctly discovers configuration files such as `.cargo/config.toml` and `toolchain.toml` in the source tree.
4+
5+
## What's Being Tested
6+
7+
Cargo discovers configuration files by searching the current working directory and its parent directories. Therefore, Corrosion must set the correct `WORKING_DIRECTORY` for all `cargo` invocations.
8+
9+
## How the Test Works
10+
11+
The test uses a `.cargo/config.toml` that defines a custom registry alias `my-registry`. A dependency in `Cargo.toml` references this alias via `registry = "my-registry"`.
12+
13+
If `cargo` does not find `.cargo/config.toml`, the `registry = "my-registry"` entry will cause an error during manifest parsing.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Defines registry alias `my-registry` pointing to crates.io.
2+
# If not discovered cargo errors: "registry `my-registry` not found"
3+
[registries.my-registry]
4+
index = "https://github.com/rust-lang/crates.io-index"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
project(test_project VERSION 0.1.0)
3+
include(../../test_header.cmake)
4+
5+
corrosion_import_crate(MANIFEST_PATH Cargo.toml)

test/config_discovery/config_discovery/Cargo.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "config_discovery"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
[dependencies]
7+
# Uses registry alias `my-registry`. If `.cargo/config.toml` is not discovered, the test will
8+
# fail wit the error: "registry `my-registry` not found"
9+
bitflags = { version = "1.3", registry = "my-registry" }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use bitflags::bitflags;
2+
3+
bitflags! {
4+
struct TestFlags: u32 {
5+
const VALUE = 0b00000001;
6+
}
7+
}
8+
9+
fn main() {
10+
let flags = TestFlags::VALUE;
11+
println!("Flag value: {}", flags.bits());
12+
}

0 commit comments

Comments
 (0)