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

fix: windows issues #446

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
7 changes: 7 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ build:ci --//build:execution_env=ci
common --enable_bzlmod
build --@cgrindel_bazel_starlib//bzlmod:enabled

# windows requires symlinks to work well
startup --windows_enable_symlinks

# this library requires runfiles for the bzlformat_lint_test
# but this is off by default on windows. Switch it on.
common --enable_runfiles

# Try to import a local.rc file; typically, written by CI
try-import %workspace%/local.bazelrc

5 changes: 5 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# The behavior of core.autocrlf=input is to force conversion to LF on addition
# into the repository and not to perform any conversion on checkout; that is,
# to always use LF endings regardless of the user's settings. This is set in
# .gitattributes as '* eol=lf'
* eol=lf
6 changes: 5 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ bazel_dep(
)
bazel_dep(
name = "buildifier_prebuilt",
version = "6.0.0.1",
version = "6.1.2",
)
bazel_dep(name = "platforms", version = "0.0.6")
bazel_dep(
name = "aspect_bazel_lib",
version = "2.0.1"
)

go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
Expand Down
1 change: 1 addition & 0 deletions bzlformat/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ bzl_library(
":bzlformat_format",
"//bzllib:defs",
"//updatesrc:defs",
"@aspect_bazel_lib//lib:windows_utils",
"@bazel_skylib//rules:diff_test",
],
)
Expand Down
17 changes: 13 additions & 4 deletions bzlformat/private/bzlformat_lint_test.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Definition for bzlformat_lint_test rule."""

load("@aspect_bazel_lib//lib:windows_utils.bzl", "create_windows_native_launcher_script")
load("@bazel_skylib//lib:shell.bzl", "shell")
load("//shlib/rules:execute_binary.bzl", "execute_binary_utils")

Expand All @@ -23,9 +24,9 @@ def _bzlformat_lint_test_impl(ctx):
lint_test_names = [lt.short_path for lt in lint_tests]

# Write a script that executes all of the lint tests
out = ctx.actions.declare_file(ctx.label.name + ".sh")
bash_launcher = ctx.actions.declare_file(ctx.label.name + ".sh")
ctx.actions.write(
output = out,
output = bash_launcher,
is_executable = True,
content = """\
#!/usr/bin/env bash
Expand Down Expand Up @@ -55,15 +56,19 @@ echo "All tests succeeded!"
""",
)

is_windows = ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])
launcher = create_windows_native_launcher_script(ctx, bash_launcher) if is_windows else bash_launcher
extra_runfiles = [bash_launcher] if is_windows else []

# Gather the runfiles
runfiles = ctx.runfiles(files = ctx.files.srcs + lint_tests)
runfiles = ctx.runfiles(files = ctx.files.srcs + lint_tests + extra_runfiles)
runfiles = execute_binary_utils.collect_runfiles(
runfiles,
[ctx.attr._buildifier],
)

# Return the DefaultInfo
return DefaultInfo(executable = out, runfiles = runfiles)
return DefaultInfo(executable = launcher, runfiles = runfiles)

bzlformat_lint_test = rule(
implementation = _bzlformat_lint_test_impl,
Expand All @@ -85,6 +90,10 @@ bzlformat_lint_test = rule(
allow_files = True,
doc = "The `buildifier` script that executes the formatting.",
),
"_windows_constraint": attr.label(default = "@platforms//os:windows"),
},
toolchains = [
"@bazel_tools//tools/sh:toolchain_type",
],
doc = "Lints the specified Starlark files using Buildifier.",
)
3 changes: 3 additions & 0 deletions tests/bzlformat_tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ _FORMAT_INFOS = [
name = fi[0] + "_bzl",
out = fi[0] + ".bzl",
content = fi[1],
newline = "unix",
)
for fi in _FORMAT_INFOS
]
Expand All @@ -64,11 +65,13 @@ bzlformat_format(
)

# Write the expected content
# buildifier writes unix newlines; match that
[
write_file(
name = fi[0] + "_bzl_expected",
out = fi[0] + ".bzl.expected",
content = fi[2],
newline = "unix",
)
for fi in _FORMAT_INFOS
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,35 @@ source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
# --- end runfiles.bash initialization v3 ---

# return a unix-style path on all platforms
# workaround for https://github.com/bazelbuild/bazel/issues/22803
function rlocation_as_unix() {
path=$(rlocation ${1})
case "$(uname -s)" in
CYGWIN* | MINGW32* | MSYS* | MINGW*)
path=${path//\\//} # backslashes to forward
path=/${path//:/} # d:/ to /d/
;;
esac
echo $path
}

# MARK - Locate Deps

assertions_sh_location=cgrindel_bazel_starlib/shlib/lib/assertions.sh
assertions_sh="$(rlocation "${assertions_sh_location}")" || \
assertions_sh="$(rlocation_as_unix "${assertions_sh_location}")" || \
(echo >&2 "Failed to locate ${assertions_sh_location}" && exit 1)
# shellcheck source=SCRIPTDIR/../../../../shlib/lib/assertions.sh
source "${assertions_sh}"

archive_tar_gz_location=cgrindel_bazel_starlib/tests/bzlrelease_tests/rules_tests/release_artifact_tests/archive.tar.gz
archive_tar_gz="$(rlocation "${archive_tar_gz_location}")" || \
archive_tar_gz="$(rlocation_as_unix "${archive_tar_gz_location}")" || \
(echo >&2 "Failed to locate ${archive_tar_gz_location}" && exit 1)


# MARK - Test

echo ${archive_tar_gz}
peakschris marked this conversation as resolved.
Show resolved Hide resolved
contents="$(tar -tf "${archive_tar_gz}")"
assert_match "bzlrelease/" "${contents}"
assert_match "bzlrelease/private/" "${contents}"
Expand Down
3 changes: 3 additions & 0 deletions updatesrc/private/updatesrc_diff_and_update.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def updatesrc_diff_and_update(
src = srcs[idx]
out = outs[idx]
src_name = src.replace("/", "_")

# this difftest fails as file2 has CRLf on windows
# fix: https://github.com/bazelbuild/bazel-skylib/pull/527
diff_test(
name = diff_test_prefix + src_name + diff_test_suffix,
file1 = src,
Expand Down
Loading