@@ -45,6 +45,14 @@ def add_arguments(self, *, parser): # noqa: D102
45
45
'By default, dependencies are looked up only in the installation '
46
46
'prefixes. This option is useful for setting up a '
47
47
'.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)' )
48
56
49
57
def _prepare (self , env , additional_hooks ):
50
58
args = self .context .args
@@ -66,7 +74,7 @@ def _prepare(self, env, additional_hooks):
66
74
# Hence, the installed package paths need to be accumulated.
67
75
new_package_paths .update (package_paths )
68
76
package_paths = new_package_paths
69
- write_cargo_config_toml (package_paths )
77
+ self . write_cargo_config_toml (package_paths )
70
78
71
79
additional_hooks += create_environment_hook (
72
80
'ament_prefix_path' ,
@@ -89,19 +97,33 @@ def _build_cmd(self, cargo_args):
89
97
'--quiet'
90
98
] + cargo_args
91
99
100
+ def write_cargo_config_toml (self , package_paths ):
101
+ """Write the resolved package paths to config.toml.
92
102
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' ))
105
127
106
128
107
129
def find_installed_cargo_packages (env ):
0 commit comments