Skip to content

Conversation

@cormacrelf
Copy link
Contributor

@cormacrelf cormacrelf commented Sep 23, 2025

Fixes #683 if you enable the config.

Also fixes #890.

This is achieved by ignoring dropping the build_file_cell during ImportPath construction. I can confirm this fixes the test case on that issue (https://github.com/cormacrelf/buck2-tset-bug).

This is a big improvement for users outside of Meta because previously it was practically impossible to use transitive sets. I have been writing some rules and have had to emulate the feature with regular starlark sets.

Previous version of this PR had cfg flags; now I made a buckconfig for it. Because it does break some things.

This will break any code that reads read_config / get_cell_name at the top level of a BZL file outside of the prelude. It will also break code that uses read_config / get_cell_name during analysis (which should never have worked). It is therefore not activated for anyone by default. However it is not super difficult to migrate. To test it, you can use

# root .buckconfig
[buck2]
    disable_cell_segmentation = true

Breakage (behind buckconfig)

Before (non-prelude cell)

# all four calls relate to the build file cell and will all agree.
STATIC_CONFIG = read_config(...)
STATIC_CELL = native.get_cell_name()
def macro():
    _ = read_config(...)
    _ = native.get_cell_name()

After

# These two relate to the .bzl file's cell
STATIC_CONFIG = read_config(...)
STATIC_CELL = native.get_cell_name()

def macro():
    # these two relate to build file cell as before
    _ = read_config(...)
    _ = native.get_cell_name()

For the prelude specifically

No change, the "after" is already how it works in the prelude.

read_config during analysis is no longer accepted

Previously this was unofficially and perhaps accidentally supported:

def _impl(ctx: AnalysisContext) -> list[Provider]:
    xyz = read_config("abc", "xyz")
    ...

rule = rule(impl = _impl)

The reason being that read_config was #[starlark(speculative_exec_safe)], so it was actually being inlined when the entire file was parsed. It can no longer be inlined so this no longer works. It fails with

BUILD FAILED
Error running analysis for `root//rust:test (prelude//platforms:default#7171795d350c5b11)`

Caused by:
    Traceback (most recent call last):
      File <builtin>, in <module>
      * prelude/rust/rust_binary.bzl:519, in rust_test_impl
          re_executor, executor_overrides = get_re_executors_from_props(ctx)
      * prelude/tests/re_utils.bzl:68, in get_re_executors_from_props
          re_arg = _get_re_arg(ctx)
      * prelude/tests/re_utils.bzl:20, in _get_re_arg
          force_local = read_config("fbcode", "disable_re_tests", default = False)

    error: This function is unavailable during analysis (usual solution is to place the information on a toolchain)
      --> prelude/tests/re_utils.bzl:20:19
       |
    20 |     force_local = read_config("fbcode", "disable_re_tests", default = False)
       |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       |

Review questions

  • Is parser.cell_segmentation the best buckconfig name? Almost every other config is in the buck2 section but this is one of those rare occasions that maybe parser is right? Feel free to change it in app/buck2_interpreter/src/import_paths.rs. You can call the config whatever you like, just fix the tests, or I am happy to change it. I suppose it doesn't matter since it's a temporary thing anyway. Ok I changed it to buck2.disable_cell_segmentation.
  • I don't know how to run the integration test that I wrote. Fingers crossed it works.
  • There may be a need for disabling this in a more fine-grained way. Especially if people are using a bunch of external cells and they have not been updated to support this new mode.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 23, 2025
@facebook-github-bot
Copy link
Contributor

@facebook-github-bot has imported this pull request. If you are a Meta employee, you can view this in D83033385. (Because this pull request was imported automatically, there will not be any future comments.)

@cormacrelf cormacrelf force-pushed the feature/no-cell-segmentation branch from 351fb34 to 58cf47a Compare September 23, 2025 08:41
@cormacrelf

This comment was marked as outdated.

@cormacrelf

This comment was marked as outdated.

@cormacrelf cormacrelf force-pushed the feature/no-cell-segmentation branch 2 times, most recently from 3978603 to 6202ba3 Compare September 23, 2025 13:21
@cormacrelf cormacrelf changed the title Disable cell-segmentation of import paths for not(fbcode_build) Disable cell-segmentation of import paths for OSS / with buckconfig Oct 24, 2025
@cormacrelf cormacrelf changed the title Disable cell-segmentation of import paths for OSS / with buckconfig Disable cell-segmentation of import paths for OSS or with buckconfig Oct 24, 2025
@cormacrelf cormacrelf force-pushed the feature/no-cell-segmentation branch 2 times, most recently from 03d687f to ae5a8f0 Compare October 24, 2025 12:57
@meta-codesync
Copy link
Contributor

meta-codesync bot commented Oct 24, 2025

@facebook-github-bot has imported this pull request. If you are a Meta employee, you can view this in D83033385. (Because this pull request was imported automatically, there will not be any future comments.)

Comment on lines +271 to +274
ImportPath::new_with_build_file_cells(
path,
self.build_file_cell,
self.cell_segmentation,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right above this code is where we first check if the path is in the prelude and use ImportPath::new_same_cell(). When cell segmentation is disabled, ImportPath::new_with_build_file_cells behaves exactly like this.

@cormacrelf cormacrelf force-pushed the feature/no-cell-segmentation branch 3 times, most recently from ff0a85f to fd0a1ce Compare October 27, 2025 01:15
@cormacrelf cormacrelf marked this pull request as draft October 27, 2025 03:03
@cormacrelf
Copy link
Contributor Author

cormacrelf commented Oct 27, 2025

Draft because basically this breaks some usages of get_cell_name being called in a .bzl file.

# one//:bfc.bzl

# returns one, only evaluated once
ACTIVE_CELL = native.get_cell_name()

def macro():
    # works
    return native.get_cell_name()

# two//BUCK
load("one//:bfc.bzl", "macro", "ACTIVE_CELL")

# if you import ACTIVE_CELL, you get "one".
# but if you call the macro, it returns "two"
# works
macro()

So we just have to fix some usages like ACTIVE_CELL to be like macro().

@cormacrelf cormacrelf force-pushed the feature/no-cell-segmentation branch from fd0a1ce to 4789797 Compare October 27, 2025 09:27
@cormacrelf

This comment was marked as duplicate.

This is needed sometimes in tests. You can just use `(&self.root_config).xxx()` but this is
hard to discover. We could also implement the LegacyBuckConfigView directly on LegacyBuckConfig
but you generally want to prohibit mutating it, hence implementing on &T in the first place.
ImportPath::new_with_build_file_cells takes a flag `cell_segmentation`
and suppresses the effect of build_file_cell if it is false.
We then plumb this boolean through buckconfig and dice to all the
locations it is needed to construct an ImportPath.

Ultimately if `buck2.disable_cell_segmentation` is true, then ImportPath
becomes a redundant wrapper around CellPath.
This helps avoid having to add more cell_segmentation parameters by reducing
the public API of ImportPath.
Needed to change the original test when experimenting
with making disable_cell_segmentation the default on OSS.
So may as well test the actual cell_segmentation behaviour.
This lets buck build itself again. These were top level get_cell_name/read_config calls
that can simply be moved into the macro call.
The comment explains why. This will cause some breakage because there is code in prelude
relying on read_config being inlined. Fortunately we have this behind a buckconfig flag.
Previously worked because of #[starlark(speculative_exec_safe)]. Move to top level instead
of calling during analysis.
@cormacrelf cormacrelf changed the title Disable cell-segmentation of import paths for OSS or with buckconfig Disable cell-segmentation of import paths with buckconfig Oct 27, 2025
@cormacrelf cormacrelf force-pushed the feature/no-cell-segmentation branch from 5a4fabe to 6328b14 Compare October 27, 2025 12:28
@cormacrelf cormacrelf marked this pull request as ready for review October 27, 2025 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type inconsistency when loading .bzl from different cells Transitive sets don't aggregate across cells

2 participants