@@ -114,14 +114,6 @@ extended configuration file as well, to pass them both to the TypeScript compile
114114 toolchains = COPY_FILE_TO_BIN_TOOLCHAINS ,
115115)
116116
117- def _filter_input_files (files , allow_js , resolve_json_module ):
118- return [
119- f
120- for f in files
121- # include typescript, json & declaration sources
122- if _lib .is_ts_src (f .basename , allow_js , resolve_json_module ) or _lib .is_typings_src (f .basename )
123- ]
124-
125117def _write_tsconfig_rule (ctx ):
126118 # TODO: is it useful to expand Make variables in the content?
127119 content = ctx .attr .content
@@ -135,22 +127,30 @@ def _write_tsconfig_rule(ctx):
135127 extends_path = "./" + extends_path
136128 content = content .replace ("__extends__" , extends_path )
137129
138- filtered_files = _filter_input_files (ctx .files .files , ctx .attr .allow_js , ctx .attr .resolve_json_module )
139-
140- # Update file paths to be relative to the tsconfig file, including a ./ prefix
141- # to ensure paths are all relative to the config file.
142- package_prefix = ctx .label .package + "/"
143-
144- # If the target is inside another workspace, we will need to also add the workspace
145- # root to the prefix.
130+ # The prefix of source files that are within the same package as the tsconfig file.
131+ local_package_prefix = "%s/" % ctx .label .package if ctx .label .package else ""
146132 if (len (ctx .label .repo_name ) > 0 ):
147- package_prefix = "../{}/{}" .format (ctx .label .repo_name , package_prefix )
148- filtered_files = [
149- "./" + f .short_path .removeprefix (package_prefix )
150- for f in filtered_files
151- ]
152-
153- content = content .replace ("\" __files__\" " , str (filtered_files ))
133+ # If the target is inside another workspace the prefix also contains the navigation to that workspace.
134+ local_package_prefix = "../{}/{}" .format (ctx .label .repo_name , local_package_prefix )
135+
136+ # The path to navigate to the root of the workspace
137+ path_to_root = "/" .join ([".." for _ in ctx .label .package .split ("/" )])
138+
139+ # Compute the list of source files with paths relative to the generated tsconfig file.
140+ src_files = []
141+ for f in ctx .files .files :
142+ # Only include typescript source files
143+ if not (_lib .is_ts_src (f .basename , ctx .attr .allow_js , ctx .attr .resolve_json_module ) or _lib .is_typings_src (f .basename )):
144+ continue
145+
146+ if f .short_path .startswith (local_package_prefix ):
147+ # Files within this project or subdirs can avoid the ugly ../ prefix
148+ src_files .append ("./{}" .format (f .short_path .removeprefix (local_package_prefix )))
149+ else :
150+ # Files from parent/sibling projects must navigate up to the workspace root
151+ src_files .append ("./{}/{}" .format (path_to_root , f .short_path ))
152+
153+ content = content .replace ("\" __files__\" " , str (src_files ))
154154 ctx .actions .write (
155155 output = ctx .outputs .out ,
156156 content = content ,
0 commit comments