Skip to content

Shell API and CLI: Add option for staging additional runtime targets - #2147

Open
nathanwilliams-ct wants to merge 2 commits into
apache:masterfrom
nathanwilliams-ct:nathan/shell-multi
Open

nathanwilliams-ct wants to merge 2 commits into
apache:masterfrom
nathanwilliams-ct:nathan/shell-multi

Conversation

@nathanwilliams-ct

@nathanwilliams-ct nathanwilliams-ct commented Jul 6, 2026 •

Copy link
Copy Markdown
Contributor

This enables users to add additional functionality such as debug tooling in the shell sandbox,
without needing to modify the target element. This is achived through introducing a new option to
shell.

All existing API and UX is maintained, to not break existing scripts. An alternative design was
considered, to have the additional elements as positional arguments similar to the existing element,
but this would need manual parsing to handle the cases where -- is present and not
present, splitting based on a .bst suffix. This UX could be re-visited in future.

Example usage:
bst shell --with base.bst example.bst -- cat example.txt
Where:

  • example.bst is a simple import element with no dependencies that imports a file called example.txt
  • base.bst provides a basic alpine sysroot with a standard set of unix tooling (sh, df, cat etc).

Changes:

  • Introduces test_with_other_targets integration test to the shell test suite.
  • Adds --with cli option to the shell subcommand and updates it's documentation.
    • option can be used multiple times by caller, providing a list of targets.
  • Extends the shell top level calling interface in Buildstream core to accept a list of
    other_targets
    • This is where the targets are loaded into elements and checked to make sure they are present
  • Extends the shell element implementation to accept a list of other targets
    • This is where the other elements are staged and integrated into the sandbox

towards: #422

@nathanwilliams-ct

Copy link
Copy Markdown
Contributor Author

Thanks, whoever triggered the CI... looks like it needs a little work, despite the tests working locally in the tox environment and I missed the linters.

@nathanwilliams-ct
nathanwilliams-ct force-pushed the nathan/shell-multi branch 4 times, most recently from 6c82853 to fb98811 Compare July 16, 2026 10:47
@nathanwilliams-ct
nathanwilliams-ct marked this pull request as ready for review July 16, 2026 12:42
@nathanwilliams-ct

Copy link
Copy Markdown
Contributor Author

I think this should pass CI now, fixed docs, linters, static type checker, formatter and 3.10 issues

@nathanwilliams-ct
nathanwilliams-ct force-pushed the nathan/shell-multi branch 3 times, most recently from 5c266ec to 4857d9e Compare July 16, 2026 14:48
@nathanwilliams-ct

Copy link
Copy Markdown
Contributor Author

pre-commit hooks would be nice. I keep running the commands locally, but It seems I am doing them in the wrong order.. fixing one thing breaks the others XD

@nathanwilliams-ct

Copy link
Copy Markdown
Contributor Author
tox
tox -- --integration
tox -e docs
tox -e lint
tox -e mypy
tox -e format

And the tests locally only run on 313 and 314 for me, while CI does also 310 311 312

Is complete list right?

@juergbi

juergbi commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

And the tests locally only run on 313 and 314 for me, while CI does also 310 311 312

tox runs the tests on all Python versions from 3.10 to 3.14 by default, but skipping versions where the interpreter is not installed. It's normally fine to test against just one Python version locally, letting CI take care of the others, unless you're actively debugging a version-specific issue.

Is complete list right?

CI also runs buildgrid and buildbarn, tests against master of buildstream-plugins-community, and builds wheels. Unless you're working in these specific areas, it shouldn't be necessary to run those locally.

@nathanwilliams-ct

Copy link
Copy Markdown
Contributor Author

Ah there's a subtle conflict between format and the docs generation.

@nathanwilliams-ct
nathanwilliams-ct force-pushed the nathan/shell-multi branch 2 times, most recently from abffc99 to 5a52dae Compare July 17, 2026 09:15
@nathanwilliams-ct

Copy link
Copy Markdown
Contributor Author

Static type checking moved out into it's own PR: #2153

Comment thread src/buildstream/element.py Outdated
Comment thread src/buildstream/_stream.py Outdated
Comment thread tests/integration/shell.py
Comment thread src/buildstream/_loader/loader.py Outdated
Comment thread src/buildstream/_stream.py Outdated

target_loader._set_fullpath_overrides(target_name, temp_target_file.name)

self.build([target])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should no longer be necessary with the fullpath override, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changing the dependencies messes with the cache key so a build is necessary still.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Direct runtime dependencies shouldn't affect the cache key. And for build shells (where we add build dependencies), we shouldn't invoke a build before entering the shell anyway. Or am I misremembering or missing something? Have you confirmed a cache key issue with the latest version of your branch (with the fullpath override)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hmmm, No, I will do some further testing.

Comment thread src/buildstream/_loader/loader.py Outdated
Comment thread src/buildstream/_frontend/cli.py
@juergbi

juergbi commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Nit: We prefer limiting normal text lines in git commit messages to 72 characters, matching usual git conventions.

@juergbi

juergbi commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

The test seems to be failing on Python 3.10 (Ubuntu 22.04) and Python 3.11 (Debian 12).

Comment thread src/buildstream/_stream.py Outdated
Comment thread src/buildstream/_loader/loader.py Outdated
This enables users to add additional functionality such as debug tooling in the shell sandbox,
without needing to modify the target element. This is achived through introducing a new option to
shell.

All existing API and UX is maintained, to not break existing scripts. An alternative design was
considered, to have the additional elements as positional arguments similar to the existing element,
but this would need manual parsing to handle the cases where `--` is present and not
present, splitting based on a `.bst` suffix. This UX could be re-visited in future.

Example usage:
  bst shell --with base.bst example.bst -- cat example.txt
Where:
- example.bst is a simple import element with no dependencies that imports a file called example.txt
- base.bst provides a basic alpine sysroot with a standard set of unix tooling (sh, df, cat etc).

Changes:
- Introduces `test_with_other_targets` integration test to the shell test suite.
- Adds `--with` cli option to the shell subcommand and updates it's documentation.
  - option can be used multiple times by caller, providing a list of targets.
- Extends the shell top level calling interface in Buildstream core to accept a list of
other_targets
  - This is where the targets are loaded into elements and checked to make sure they are present
- Extends the shell element implementation to accept a list of other targets
  - This is where the other elements are staged and integrated into the sandbox

@juergbi juergbi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the updates. I think it's almost ready but have a few minor comments.

Comment on lines +18 to +19
import sys
import tempfile

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

src/buildstream/_loader/loader.py:18:0: W0611: Unused import sys (unused-import)
src/buildstream/_loader/loader.py:19:0: W0611: Unused import tempfile (unused-import)

ignore_project_artifact_remotes=ignore_project_artifact_remotes,
ignore_project_source_remotes=ignore_project_source_remotes,
)
if other_targets:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Either here or in Stream.shell_with() we should reject the combination of --use-buildtree and --with with a clear error message as this combination can't work, right?

Comment on lines +305 to +309
def _set_fullpath_override(self, filename: str, fullpath: str | None):
if fullpath:
self._fullpath_overrides[filename] = fullpath
else:
self._fullpath_overrides.pop(filename, None)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: Each branch is a one-liner and used exactly once. Wouldn't it be simpler to inline it at the call site?

If you think it's clearer to have this in a separate method (it could hypothetically also be used for other purposes), then I think this should be a context manager to provide some actual benefit at the call site.

Either variant is fine with me.

# Test staging and running additional targets in the shell of the main target for debugging.
@pytest.mark.datafiles(DATA_DIR)
@pytest.mark.skipif(not HAVE_SANDBOX, reason="Only available with a functioning sandbox")
def test_with_other_targets(cli, datafiles):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd like to see at least a minimal test for a build shell as well.

else:
raise StreamError(
"Only BUILD and RUN scopes are supported",
detail="Use the --build and --use-buildtree options to shell into a build tree",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This detail doesn't make sense to me. Leftover from copy paste?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants