Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow error messages to be reported in Sass workers #121

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ def rules_sass_dependencies():
_include_if_not_defined(
http_archive,
name = "build_bazel_rules_nodejs",
sha256 = "a54b2511d6dae42c1f7cdaeb08144ee2808193a088004fc3b464a04583d5aa2e",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/0.42.3/rules_nodejs-0.42.3.tar.gz"],
sha256 = "0f2de53628e848c1691e5729b515022f5a77369c76a09fbe55611e12731c90e3",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/2.0.1/rules_nodejs-2.0.1.tar.gz"],
)

# Dependencies from the NodeJS rules. We don't want to use the "package.bzl" dependency macro
Expand Down
5 changes: 3 additions & 2 deletions sass/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ exports_files([
# Executable for the sass_binary rule
nodejs_binary(
name = "sass",
entry_point = "sass_wrapper",
data = [
":sass_wrapper.js",
"@build_bazel_rules_sass_deps//sass",
"@build_bazel_rules_sass_deps//@bazel/worker",
"@build_bazel_rules_sass_deps//minimist",
"@build_bazel_rules_sass_deps//sass",
],
entry_point = "sass_wrapper.js",
)
1 change: 1 addition & 0 deletions sass/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"devDependencies": {
"@bazel/worker": "latest",
"minimist": "1.2.5",
"sass": "1.26.10"
}
}
22 changes: 12 additions & 10 deletions sass/sass.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
# limitations under the License.
"Compile Sass files to CSS"

load("@build_bazel_rules_nodejs//:providers.bzl", "NodeRuntimeDepsInfo", "run_node")

_ALLOWED_SRC_FILE_EXTENSIONS = [".sass", ".scss", ".css", ".svg", ".png", ".gif", ".cur", ".jpg", ".webp"]

# Documentation for switching which compiler is used
Expand Down Expand Up @@ -104,10 +106,11 @@ def _run_sass(ctx, input, css_output, map_output = None):
args.use_param_file("@%s", use_always = True)
args.set_param_file_format("multiline")

ctx.actions.run(
run_node(
ctx,
mnemonic = "SassCompiler",
executable = ctx.executable.compiler,
inputs = _collect_transitive_sources([input], ctx.attr.deps),
executable = "compiler",
inputs = _collect_transitive_sources([input], ctx.attr.deps).to_list(),
tools = [ctx.executable.compiler],
arguments = [args],
outputs = [css_output, map_output] if map_output else [css_output],
Expand Down Expand Up @@ -239,10 +242,8 @@ sass_binary = rule(

def _multi_sass_binary_impl(ctx):
"""multi_sass_binary accepts a list of sources and compile all in one pass.

Args:
ctx: The Bazel build context

Comment on lines -242 to -245
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why these cosmetic changes?

Returns:
The multi_sass_binary rule.
"""
Expand All @@ -268,26 +269,27 @@ def _multi_sass_binary_impl(ctx):
))

# Use the package directory as the compilation root given to the Sass compiler
root_dir = (ctx.label.workspace_root + "/" if ctx.label.workspace_root else "") + ctx.label.package
load_dir = (ctx.label.workspace_root + "/" if ctx.label.workspace_root else "") + ctx.label.package

# Declare arguments passed through to the Sass compiler.
# Start with flags and then expected program arguments.
args = ctx.actions.args()
args.add("--style", ctx.attr.output_style)
args.add("--load-path", root_dir)
args.add("--load-path", load_dir)

if not ctx.attr.sourcemap:
args.add("--no-source-map")

args.add(root_dir + ":" + ctx.bin_dir.path + '/' + root_dir)
args.add(load_dir + ":" + ctx.bin_dir.path + '/' + load_dir)
args.use_param_file("@%s", use_always = True)
args.set_param_file_format("multiline")

if inputs:
ctx.actions.run(
run_node(
ctx,
inputs = inputs,
outputs = outputs,
executable = ctx.executable.compiler,
executable = "compiler",
arguments = [args],
mnemonic = "SassCompiler",
progress_message = "Compiling Sass",
Expand Down
3 changes: 2 additions & 1 deletion sass/sass_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ def sass_repositories():

# 0.31.1: entry_point attribute of rules_nodejs is now a label
# 0.32.0: @npm//node_modules/foobar:foobar.js labels changed to @npm//:node_modules/foobar/foobar.js with fix for bazelbuild/rules_nodejs#802.
check_rules_nodejs_version("0.32.0")
# 2.0.1: run_node exported as part of its external API
check_rules_nodejs_version("2.0.1")

yarn_install(
name = "build_bazel_rules_sass_deps",
Expand Down
35 changes: 34 additions & 1 deletion sass/sass_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,44 @@
const {debug, runAsWorker, runWorkerLoop} = require('@bazel/worker');
const sass = require('sass');
const fs = require('fs');
const minimist = require('minimist');

const args = process.argv.slice(2);
if (runAsWorker(args)) {
debug('Starting Sass compiler persistent worker...');
runWorkerLoop(args => sass.cli_pkg_main_0_(args));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the goal of this change? It adds a lot of complexity and requires us to update multiple places any time we want to change which arguments are passed in to the compiler.

Copy link
Contributor Author

@BobobUnicorn BobobUnicorn Sep 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's to address #96. The issue is that the Dart _Future is not directly translatable to JS promises, so the worker loop doesn't have any idea when the compilation is actually done (or if it succeeded). At present, it just treats the unknown object as "truthy".

I couldn't find a way to wrap a Future in a Promise on the JS side; it may have to be done in Dart.

One other option could be to try and farm out to an execFile so we can just pass the arguments directly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather fix the root cause by adding support to http://github.com/google/dart_cli_pkg for wrapping futures in Promises than make this package harder to maintain going forward.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing that's google/dart_cli_pkg#50? I'm happy to take a look there; let me shelve this for now then.

runWorkerLoop(args => {
const argv = minimist(args);
const positionalArgs = argv['_'];
const output = positionalArgs[1];
const input = positionalArgs[0];

const sourceMap =
typeof argv['source-map'] === 'boolean' ? argv['source-map'] : true;
const embedSources =
typeof argv['embed-sources'] === 'boolean'
? argv['embed-sources']
: false;

try {
const result = sass.renderSync({
file: input,
outFile: output,
includePaths: argv['load-path'],
outputStyle: argv['style'],
sourceMap,
sourceMapContents: embedSources
});

fs.writeFileSync(output, result.css);
if (sourceMap) {
fs.writeFileSync(output + '.map', result.map);
}
return true;
} catch (e) {
console.error(e.message);
return false;
}
});
// Note: intentionally don't process.exit() here, because runWorkerLoop
// is waiting for async callbacks from node.
} else {
Expand Down
5 changes: 5 additions & 0 deletions sass/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ long@^4.0.0:
resolved "https://registry.yarnpkg.com/long/-/long-4.0.0.tgz#9a7b71cfb7d361a194ea555241c92f7468d5bf28"
integrity sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==

[email protected]:
version "1.2.5"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602"
integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==

normalize-path@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-3.0.0.tgz#0dcd69ff23a1c9b11fd0978316644a0388216a65"
Expand Down