Skip to content

Windows: "data" dependencies does not link to ".runfiles" #3881

Closed

Description

@eustas

If possible, provide a minimal example to reproduce the problem:

BUILD file:

filegroup(
    name = "some_data",
    srcs = ["some_data.txt"],
)
java_test(
    name = "SomeDataTest",
    srcs = ["SomeDataTest.java"],
    data = [":some_data"],
    jvm_flags = ["-DSOME_DATA=$(location :some_data)"],
    deps = ["@junit_junit//jar"],
)

Result: SomeDataTest can't read file at System.getProperty("SOME_DATA")
Path looks OK, but there is no corresponding file in .runfiles.

Environment info

  • Operating System:
    Fails on Windows
    Works fine on OSX, Linux

  • Bazel version (output of bazel info release):
    0.6.1 windows binary distro

Activity

  1. eustas commented on Oct 10, 2017

    @eustas
    Author

    Correct entry appears in .runfiles_manifest. But this doesn't help at all, as there is no API to use it.

  2. dslomov commented on Oct 10, 2017

    @dslomov
    Contributor

    We do not have an API to read .runfiles_manifest but it is fairly trivial:

  3. eustas commented on Oct 10, 2017

    @eustas
    Author

    Thanks.
    Unfortunately this solution does not align nicely with "multiple-build-system" concept: use different build systems should use the same source code.

  4. dslomov commented on Oct 10, 2017

    @dslomov
    Contributor

    We are open to suggestions on how to fix it.
    Creating symlinks on Windows is not really an option as it requires admin privileges.

  5. eustas commented on Oct 10, 2017

    @eustas
    Author

    Perhaps, least intrusive way is to update $location behavior, or make an alternative to it.
    Since reading files pointed in manifest already breaks the concept of sandboxing, it won't make the things worse, if $location would point to absolute file path.
    Such change would instantly fix my project build on Windows (without touching neither BUILD files, nor sources)

  6. added
    P3
    on Oct 10, 2017
  7. dslomov commented on Oct 10, 2017

    @dslomov
    Contributor

    Since reading files pointed in manifest already breaks the concept of sandboxing,

    No, it does not. In the same way, under Linux inside sandbox the application can use readlink to discover where symlink points - that does not "break the concept of sandboxing".

    it won't make the things worse, if $location would point to absolute file path.

    No it would. The result of $location is a part of a command line generated during analysis. That command line should be "relocatable", so no absolute paths.

    However one possible way to allow automatic translation might be within the API of (just added) Args object

  8. eustas commented on Oct 10, 2017

    @eustas
    Author

    It seems that Args object should fit my requirements. Thanks.

  9. dslomov commented on Oct 10, 2017

    @dslomov
    Contributor

    It seems that Args object should fit my requirements. Thanks.

    I am not saying we are doing this yet, so do not hold your breath, but I'll think about it.

  10. eustas commented on Oct 11, 2017

    @eustas
    Author

    How about optional copying of data deps when symlinking is not available (windows)?

    It seems that there is no way to hack this around using just Skylark. The only way to alter build graph using the information form the outer world is exec_result, but it is available only for repository rules...

    Other solution could be creating resource file with environment info, but it touches project sources too much (almost the same way as reading manifest) =(

  11. eustas commented on Oct 11, 2017

    @eustas
    Author

    Another tiny problem with runfiles resolving is that "workspace" path part should be either guessed or ignored. Two lines of code, but also a potential bug, as there could be name collisions.

  12. eustas commented on Oct 11, 2017

    @eustas
    Author

    My current workaround:

    import fnmatch
    import os
    import os.path
    from shutil import copyfile
    
    matches = []
    # replace with your project path
    for root, dirnames, filenames in os.walk('bazel-bin\\java\\org\\brotli'):
      for filename in fnmatch.filter(filenames, '*.runfiles_manifest'):
        matches.append(os.path.join(root, filename))
    
    for match in matches:
      runfiles = match[:-len('_manifest')]
      with open(match) as manifest:
        for entry in manifest:
          entry = entry.strip()
          if not entry.startswith("org_brotli"): # workspace id
            continue
          if entry.startswith('org_brotli/external'): #workspace id
            continue
          (alias, space, link) = entry.partition(' ')
          if alias.endswith('.jar') or alias.endswith('.exe'):
            continue
          link = link.replace('/', '\\')
          alias = alias.replace('/', '\\')
          dst = os.path.join(runfiles, alias)
          if not os.path.exists(dst):
            print(link + ' -> ' + dst)
            parent = os.path.dirname(dst)
            if not os.path.exists(parent):
              os.makedirs(parent)
            copyfile(link, dst)
    

    Just run it between build and test.

  13. added
    area-Windows
    and removed on Dec 10, 2018
  14. laszlocsomor commented on Feb 28, 2019

    @laszlocsomor
    Contributor

    I'm closing this issue, because Bazel is working fine.
    $(location) returns the runfiles path of the target. You have to pass that to the runfiles library's rlocation function. Here's more info:

    USAGE:
    1. Depend on this runfiles library from your build rule:
    py_binary(
    name = "my_binary",
    ...
    deps = ["@bazel_tools//tools/python/runfiles"],
    )
    2. Import the runfiles library.
    from bazel_tools.tools.python.runfiles import runfiles
    3. Create a Runfiles object and use rlocation to look up runfile paths:
    r = runfiles.Create()
    ...
    with open(r.Rlocation("my_workspace/path/to/my/data.txt"), "r") as f:
    contents = f.readlines()
    ...
    The code above creates a manifest- or directory-based implementations based
    on the environment variables in os.environ. See `Create()` for more info.
    If you want to explicitly create a manifest- or directory-based
    implementations, you can do so as follows:
    r1 = runfiles.CreateManifestBased("path/to/foo.runfiles_manifest")
    r2 = runfiles.CreateDirectoryBased("path/to/foo.runfiles/")
    If you want to start subprocesses that also need runfiles, you need to set
    the right environment variables for them:
    import subprocess
    from bazel_tools.tools.python.runfiles import runfiles
    r = runfiles.Create()
    env = {}
    ...
    env.update(r.EnvVars())
    p = subprocess.Popen([r.Rlocation("path/to/binary")], env, ...)
    """

  15. added
    team-OSS
    on Jun 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3area-Windowsplatform: windowsteam-OSStype: feature request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions