Skip to content

Commit ced2c78

Browse files
committed
Add argument to select .cargo/config.toml path
1 parent 73289b2 commit ced2c78

File tree

1 file changed

+35
-13
lines changed
  • colcon_ros_cargo/task/ament_cargo

1 file changed

+35
-13
lines changed

colcon_ros_cargo/task/ament_cargo/build.py

+35-13
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ def add_arguments(self, *, parser): # noqa: D102
4545
'By default, dependencies are looked up only in the installation '
4646
'prefixes. This option is useful for setting up a '
4747
'.cargo/config.toml for subsequent builds with cargo.')
48+
parser.add_argument(
49+
'--config-path',
50+
type=Path,
51+
help='The path to store the .cargo/config.toml '
52+
'By default, the configuration will be stored at the '
53+
'colcon workspace top level directory. Use this option to '
54+
'indicate a path above the resolved path of the package sources '
55+
'(e.g the pointed path if the source is under a symbolic link)')
4856

4957
def _prepare(self, env, additional_hooks):
5058
args = self.context.args
@@ -66,7 +74,7 @@ def _prepare(self, env, additional_hooks):
6674
# Hence, the installed package paths need to be accumulated.
6775
new_package_paths.update(package_paths)
6876
package_paths = new_package_paths
69-
write_cargo_config_toml(package_paths)
77+
self.write_cargo_config_toml(package_paths)
7078

7179
additional_hooks += create_environment_hook(
7280
'ament_prefix_path',
@@ -89,19 +97,33 @@ def _build_cmd(self, cargo_args):
8997
'--quiet'
9098
] + cargo_args
9199

100+
def write_cargo_config_toml(self, package_paths):
101+
"""Write the resolved package paths to config.toml.
92102
93-
def write_cargo_config_toml(package_paths):
94-
"""Write the resolved package paths to config.toml.
95-
96-
:param package_paths: A mapping of package names to paths
97-
"""
98-
patches = {pkg: {'path': str(path)} for pkg, path in package_paths.items()}
99-
content = {'patch': {'crates-io': patches}}
100-
config_dir = Path.cwd() / '.cargo'
101-
config_dir.mkdir(exist_ok=True)
102-
cargo_config_toml_out = config_dir / 'config.toml'
103-
cargo_config_toml_out.unlink(missing_ok=True)
104-
toml.dump(content, cargo_config_toml_out.open('w'))
103+
:param package_paths: A mapping of package names to paths
104+
"""
105+
args = self.context.args
106+
src_dir = Path(self.context.pkg.path)
107+
patches = {pkg: {'path': str(path)} for pkg, path in package_paths.items()}
108+
content = {'patch': {'crates-io': patches}}
109+
if args.config_path:
110+
config_dir = args.config_path.resolve() / '.cargo'
111+
else:
112+
config_dir = Path.cwd() / '.cargo'
113+
# The current package directory might be a link to another directory.
114+
# However, cargo only looks for configurations in the package directory
115+
# and in all its parent directories.
116+
# Hence, if the package directory is link, ./cargo/config.toml
117+
# should be installed above the directory pointed to
118+
# in order cargo to necessarily hit it.
119+
if src_dir.absolute() != src_dir.resolve():
120+
logger.warn('The package source path may be under a symbolic link. '
121+
'Use --config-path option to store .cargo/config.toml '
122+
'in a way it will be hit by cargo')
123+
config_dir.mkdir(exist_ok=True)
124+
cargo_config_toml_out = config_dir / 'config.toml'
125+
cargo_config_toml_out.unlink(missing_ok=True)
126+
toml.dump(content, cargo_config_toml_out.open('w'))
105127

106128

107129
def find_installed_cargo_packages(env):

0 commit comments

Comments
 (0)