From c312c685f036dc82315c33026a82f1608215c2e5 Mon Sep 17 00:00:00 2001 From: Reid 'arrdem' McKenzie Date: Sat, 27 May 2023 18:55:38 -0600 Subject: [PATCH] Make import paths work right --- tools/sass/defs.bzl | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/tools/sass/defs.bzl b/tools/sass/defs.bzl index e180c8c..8de414d 100644 --- a/tools/sass/defs.bzl +++ b/tools/sass/defs.bzl @@ -33,12 +33,12 @@ https://github.com/dart-lang/rules_dart). SassInfo = provider( doc = "Collects files from sass_library for use in downstream sass_binary", fields = { + "transitive_includes": "Directories to be treated as @use/@import search paths", "transitive_sources": "Sass sources for this target and its dependencies", }, ) def _collect_transitive_sources(srcs, deps): - "Sass compilation requires all transitive .sass source files" return depset( srcs, transitive = [dep[SassInfo].transitive_sources for dep in deps], @@ -46,6 +46,13 @@ def _collect_transitive_sources(srcs, deps): order = "postorder", ) +def _collect_transitive_imports(imports, deps): + return depset( + imports, + transitive = [dep[SassInfo].transitive_includes for dep in deps], + order = "postorder", + ) + def _sass_library_impl(ctx): """sass_library collects all transitive sources for given srcs and deps. @@ -61,8 +68,16 @@ def _sass_library_impl(ctx): ctx.files.srcs, ctx.attr.deps, ) + transitive_includes = _collect_transitive_imports( + [ctx.label.package] + ctx.attr.imports, + ctx.attr.deps, + ) + return [ - SassInfo(transitive_sources = transitive_sources), + SassInfo( + transitive_sources = transitive_sources, + transitive_includes = transitive_includes, + ), DefaultInfo( files = transitive_sources, runfiles = ctx.runfiles(transitive_files = transitive_sources), @@ -84,11 +99,8 @@ def _run_sass(ctx, input, css_output, map_output = None): if ctx.attr.sourcemap_embed_sources: args.add("--sourcemap-contents") - # Sources for compilation may exist in the source tree, in bazel-bin, or bazel-genfiles. - for prefix in [".", ctx.var["BINDIR"], ctx.var["GENDIR"]]: - args.add("--include-path=%s/" % prefix) - for include_path in ctx.attr.include_paths: - args.add("--include-path=%s/%s" % (prefix, include_path)) + for include_path in _collect_transitive_imports([ctx.label.package], ctx.attr.deps).to_list(): + args.add("--include-path=%s/" % include_path) # Last arguments are input and output paths # Note that the sourcemap is implicitly written to a path the same as the @@ -163,6 +175,9 @@ _sass_library_attrs = { allow_empty = False, mandatory = True, ), + "imports": attr.string_list( + doc = "Import prefixes", + ), "deps": sass_deps_attr, } @@ -179,6 +194,9 @@ _sass_binary_attrs = { mandatory = True, allow_single_file = _ALLOWED_SRC_FILE_EXTENSIONS, ), + "imports": attr.string_list( + doc = "Import prefixes", + ), "sourcemap": attr.bool( default = True, doc = "Whether source maps should be emitted.",