bazel icon indicating copy to clipboard operation
bazel copied to clipboard

MODULE.bazel.lock file contains user specific paths

Open jsharpe opened this issue 2 years ago • 22 comments

Description of the bug:

If you use a workspace local registry then the attributes recorded in the lockfile contain the expanded value for %workspace% instead of using the %workspace% placeholder.

Which category does this issue belong to?

External Dependency

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Use --registry=file://%workspace%/registry and then depend on a module in that registry.

The applied patches from the registry are referenced using the expanded path of %workspace% instead of using a placeholder.

Which operating system are you running Bazel on?

Ubuntu linux

What is the output of bazel info release?

6.4.0rc1

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

jsharpe avatar Sep 25 '23 15:09 jsharpe

@bazel-io flag

jsharpe avatar Sep 25 '23 15:09 jsharpe

@bazel-io fork 6.4.0

iancha1992 avatar Sep 25 '23 15:09 iancha1992

A fix for this issue has been included in Bazel 6.4.0 RC2. Please test out the release candidate and report any issues as soon as possible. Thanks!

iancha1992 avatar Oct 10 '23 21:10 iancha1992

@iancha1992 @SalmaSamy The issue is still not fixed for extensions. See https://github.com/bazelbuild/bazel/blob/8e8ddaba0e90c280bfd85644d6ccd91df5b6d353/src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleFileGlobals.java#L520

BoleynSu avatar Jan 06 '24 15:01 BoleynSu

@BoleynSu Can you please give more details of what's still broken? Ideally with a minimal reproducible case.

meteorcloudy avatar Jan 08 '24 13:01 meteorcloudy

      "extensionUsages": [
        { 
          "extensionBzlFile": "@rules_java//java:extensions.bzl",
          "extensionName": "toolchains",
          "usingModule": "[email protected]",
          "location": {
            "file": "file://PATH_TO_LOCAL_REGISTRY/modules/rules_java/7.3.2/MODULE.bazel",         
            "line": 19,
            "column": 27
          },
          "imports": {
            "remote_java_tools": "remote_java_tools",
            "remote_java_tools_linux": "remote_java_tools_linux",
            "remote_java_tools_windows": "remote_java_tools_windows",
            "remote_java_tools_darwin_x86_64": "remote_java_tools_darwin_x86_64",

It should be repro-able by building any repo with --registry=file://some_local_checkout_of_bcr.

BoleynSu avatar Jan 08 '24 14:01 BoleynSu

This is working as intended. If you're using a local path registry, we have to write the path to the lockfile to ensure reproducibility (unless the path is under the workspace root). If you're just doing some local testing and don't want to commit things into the lockfile, please use --lockfile_mode=off.

Wyverald avatar Jan 08 '24 21:01 Wyverald

Sorry that I did not make it claer. The local path is within workspace, i.e. %workspace%/reg.

BoleynSu avatar Jan 09 '24 10:01 BoleynSu

@SalmaSamy could you try to repro and investigate? And close if unreproducible.

Wyverald avatar Jan 09 '24 20:01 Wyverald

Friendly ping. The issue is not resolved yet in Bazel 7.0.1. To repro, can bazel build --config=bzlmod --lockfile_mode=update using https://github.com/BoleynSu-Org/monorepo/

BoleynSu avatar Jan 20 '24 05:01 BoleynSu

@BoleynSu Thanks, I was able to reproduce it and working on fixing it.

SalmaSamy avatar Jan 22 '24 16:01 SalmaSamy

Should this be cherry-picked into 7.0.1?

fmeum avatar Jan 23 '24 21:01 fmeum

@bazel-io fork 7.1.0

Wyverald avatar Jan 23 '24 22:01 Wyverald

A fix for this issue has been included in Bazel 7.1.0 RC1. Please test out the release candidate and report any issues as soon as possible. Thanks!

iancha1992 avatar Feb 22 '24 23:02 iancha1992

Hi @iancha1992, we are hitting this on 7.1.1 still.

Below is a snippet of the MODULE.bazel.lock. You can see it uses the %workspace%/registry registry for rules_qnx, which is an internal ruleset that lives inside the monorepo under %workspace%/modules/rules_qnx.

    "[email protected]": {
      "name": "rules_qnx",
      "version": "0.0.1",
      "key": "[email protected]",
      "repoName": "rules_qnx",
      "executionPlatformsToRegister": [],
      "toolchainsToRegister": [],
      "extensionUsages": [
        {
          "extensionBzlFile": "@rules_qnx//qnx:extensions.bzl",
          "extensionName": "patched_repo",
          "usingModule": "[email protected]",
          "location": {
            "file": "file://%workspace%/registry/modules/rules_qnx/0.0.1/MODULE.bazel",
            "line": 10,
            "column": 29
          },
          "imports": {
            "qnx_cc_toolchain_config": "qnx_cc_toolchain_config"
          },
          "devImports": [],
          "tags": [
            {
              "tagName": "create",
              "attributeValues": {
                "name": "qnx_cc_toolchain_config",
                "out": "qnx_cc_toolchain_config.bzl",
                "patch": "@rules_qnx//toolchain:cc_toolchain_config_bzl.patch",
                "upstream": "@bazel_tools//tools/cpp:unix_cc_toolchain_config.bzl"
              },
              "devDependency": false,
              "location": {
                "file": "file://%workspace%/registry/modules/rules_qnx/0.0.1/MODULE.bazel",
                "line": 14,
                "column": 20
              }
            }
          ],
          "hasDevUseExtension": false,
          "hasNonDevUseExtension": true
        }
      ],
      "deps": {
        "bazel_skylib": "[email protected]",
        "rules_cc": "[email protected]",
        "platforms": "[email protected]",
        "bazel_tools": "bazel_tools@_",
        "local_config_platform": "local_config_platform@_"
      },
      "repoSpec": {
        "ruleClassName": "local_repository",
        "attributes": {
          "path": "/home/username/monorepo/modules/rules_qnx"
        }
      }
    },

Here is the content MODULE.bazel of rules_qnx:

module(
    name = "rules_qnx",
    version = "0.0.1",
)

bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "platforms", version = "0.0.7")

patched_repo = use_extension("//qnx:extensions.bzl", "patched_repo")

# This creates a tiny repo containing a patched version of the `unix_cc_toolchain_config.bzl` file.
# The patch adds a "requires-network" tag on all compile+link actions, which is necessary for the QNX toolchain to talk to the license server without being blocked by the sandbox.
patched_repo.create(
    name = "qnx_cc_toolchain_config",
    out = "qnx_cc_toolchain_config.bzl",
    patch = "@rules_qnx//toolchain:cc_toolchain_config_bzl.patch",
    upstream = "@bazel_tools//tools/cpp:unix_cc_toolchain_config.bzl",
)
use_repo(patched_repo, "qnx_cc_toolchain_config")

I'm hoping this is enough information to help you reproduce/fix the issue?

lalten avatar Mar 27 '24 15:03 lalten

@lalten What is your expected behaviour?

meteorcloudy avatar Mar 28 '24 10:03 meteorcloudy

The "path": "/home/username/monorepo/modules/rules_qnx" part is not portable. Probably something like "path": "%workspace%/modules/rules_qnx" would be better. For now we cannot use the Lockfile because of the absolute path that's not present on most machines.

lalten avatar Mar 28 '24 11:03 lalten

Oh, sorry, I missed that.

      "repoSpec": {
        "ruleClassName": "local_repository",
        "attributes": {
          "path": "/home/username/monorepo/modules/rules_qnx"
        }
      }

But why is the root module a local_repository? /cc @SalmaSamy @Wyverald

@lalten Can you create a full reproducible case for this?

meteorcloudy avatar Mar 28 '24 11:03 meteorcloudy

I'm having the same issue, when I use my local registry relative to my %workspace%, and the MODULE.bazel.lock ends up storing the full absolute path in the "attribute".

The directory structure:

root |--- bazel_registry / modules / repo_A |--- repo_A |--- repo_B

And repo_B depends on repo_A through bazel_registry.

In repo_B/.bazelrc, I have:

common --registry=file://%workspace%/../bazel_registry
common --registry=https://bcr.bazel.build

In bazel_registry/bazel_registry.json, I have:

{
  "module_base_path": "../"
}

In bazel_registry/modules/repo_A/1.0.0/source.json, I have:

{
  "type": "local_path",
  "path": "repo_A"
}

This works correctly locally, and Bazel correctly finds the /root/repo_A and uses it when I build repo_B.

But the MODULE.bazel.lock file contains the absolute full path of repo_A in the "attribute":

      "repoSpec": {
        "ruleClassName": "local_repository",
        "attributes": {
          "path": "/Users/<my-local-directory>/repo_A"
        }
      }

This makes it not portable on a different machine, because the local path is different. Ideally the resolved attributes would be relative to %workspace%. According to bazel document, the source path would be resolved as:

If path and module_base_path are both relative paths, it resolves to <registry_path>/<module_base_path>/. Registry must be hosted locally and used by --registry=file://<registry_path>

So something like "%workspace%/../bazel_registry/../repo_A" would be a direct translation of the rules. Maybe supporting relative path so we will end up with "../repo_A" in the "attribute" is even better.

appthumb avatar Mar 30 '24 19:03 appthumb

Also, I'm using bazel 7.1.1.

$ bazelisk --version
bazel 7.1.1

appthumb avatar Mar 30 '24 19:03 appthumb

@SalmaSamy Can you take a look?

This seems to happen when source.json is using local_path

{
  "type": "local_path",
  "path": "repo_A"
}

meteorcloudy avatar Apr 03 '24 11:04 meteorcloudy

@SalmaSamy I just noticed that this would go away entirely with the new lockfile format as neither local registry contents nor any repo specs for Bazel modules are tracked in it.

fmeum avatar Apr 26 '24 15:04 fmeum

A fix for this issue has been included in Bazel 7.2.0 RC1. Please test out the release candidate and report any issues as soon as possible. If you're using Bazelisk, you can point to the latest RC by setting USE_BAZEL_VERSION=7.2.0rc1. Thanks!

iancha1992 avatar May 16 '24 00:05 iancha1992

Thanks for the fix! It works smoothly with my local bazel registry, and the MODULE.bazel.lock doesn't include any local path and works fine across different workspaces. Interestingly, when I look at the lock file, the dependency on my local repo is not mentioned at all, which seems to be intended according to https://github.com/bazelbuild/bazel/issues/19621#issuecomment-2079621790.

Overall this is very nice, thanks!

appthumb avatar May 16 '24 12:05 appthumb