feature #5609 Support `WITH NAME` alias for Resource file imports by swapneilbasutkar · Pull Request #5632 · robotframework/robotframework · GitHub
Skip to content

feature #5609 Support WITH NAME alias for Resource file imports#5632

Open
swapneilbasutkar wants to merge 1 commit into
robotframework:masterfrom
swapneilbasutkar:feature/resource-import-alias
Open

feature #5609 Support WITH NAME alias for Resource file imports#5632
swapneilbasutkar wants to merge 1 commit into
robotframework:masterfrom
swapneilbasutkar:feature/resource-import-alias

Conversation

@swapneilbasutkar

Copy link
Copy Markdown

This PR introduces the ability to import Resource files with an alias using the WITH NAME syntax, bridging the feature parity gap with Library imports. This allows users to safely namespace their imported resource files to avoid keyword naming collisions.

How it works:

  1. Parser & Lexer Updates: We updated the syntax rules so that the parser recognizes and extracts the WITH NAME alias when reading a [Resource].
  2. Execution Engine Updates: We modified the internal import runner to safely pass the alias down to the resource cache.
  3. Namespace Isolation: When a resource is imported with an alias, the execution engine now dynamically applies that exact alias as the object's namespace prefix in memory. We also updated the caching logic to store resources under a unique combination of their file path and their alias. This guarantees that importing the same resource multiple times with different aliases won't overwrite each other, while keeping 100% backward compatibility for all existing un-aliased imports.

Testing

Added a new, isolated acceptance test suite to guarantee that keywords from resource files are successfully locked under their new alias prefix at runtime without impacting legacy tests.
Feature #5609

@swapneilbasutkar swapneilbasutkar changed the title Support WITH NAME alias for Resource file imports feature #5609 Support WITH NAME alias for Resource file imports Mar 21, 2026

@themavik themavik left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Lexer/model/runner wiring for Resource + WITH NAME looks consistent with Library. nit: confirm duplicate imports of the same path without alias still dedupe on path only (alias=None branch) so existing suites do not load the resource twice.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR adds support for importing Robot Framework Resource files with an alias using the WITH NAME/AS marker, aligning Resource imports with existing Library import aliasing, and adds acceptance coverage for the new behavior.

Changes:

  • Extend settings lexing/parsing and running model to carry an optional alias for Resource imports.
  • Update runtime namespace/resource caching to support distinct imports per (path, alias) and apply the alias as the resource’s runtime namespace name.
  • Add new acceptance test suite validating aliased resource keyword usage.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/robot/running/resourcemodel.py Adds runtime support for overriding a resource’s name (namespace) and threads alias into resource import objects.
src/robot/running/namespace.py Applies resource alias during import and changes resource cache keying to incorporate alias.
src/robot/running/builder/transformers.py Passes parsed resource alias through to running model imports.
src/robot/parsing/model/statements.py Adds alias support to ResourceImport statement creation and model access.
src/robot/parsing/lexer/settings.py Allows Resource settings to use WITH NAME/AS marker similarly to Library.
atest/testdata/core/resource_alias.robot New test data verifying keyword namespace behavior with an aliased resource import.
atest/robot/core/resource_alias.robot New atest runner suite to execute the new resource alias test data.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 33 to 55
@@ -48,11 +47,11 @@ class Settings(ABC):
"Setup",
"Teardown",
"Template",
"Resource",
"Variables",
)
name_arguments_and_with_name = (
"Library",
"Resource",
) # fmt: skip

Copilot AI Mar 25, 2026

Copy link

Choose a reason for hiding this comment

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

Resource was removed from single_value so Resource settings with extra cells (e.g. Resource path extra) will no longer error during lexing and the extra values will be silently ignored later (because ResourceImport has no args). This is a behavioral regression and also breaks existing acceptance coverage that expects an error for invalid resource import parameters. Consider adding explicit validation for Resource so it accepts only Resource <path> or Resource <path> WITH NAME/AS <alias> and errors for anything else (either here in _validate() or in ResourceImport.validate()).

Copilot uses AI. Check for mistakes.
Comment on lines +99 to 106
key = (path, import_.alias) if import_.alias else path
if overwrite or key not in self._kw_store.resources:
resource = IMPORTER.import_resource(path, self.languages)
if import_.alias:
resource = resource.copy(name=import_.alias)
self.variables.set_from_variable_section(resource.variables, overwrite)
self._kw_store.resources[path] = resource
self._kw_store.resources[key] = resource
self._handle_imports(resource.imports)

Copilot AI Mar 25, 2026

Copy link

Choose a reason for hiding this comment

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

When import_.alias is set, the cache key changes to (path, alias) and the code re-runs set_from_variable_section() and _handle_imports() even if the same resource file was already imported under a different alias. This can re-define variables (potentially causing DataErrors when overwrite=False) and can also lead to duplicate keyword candidates from the same source.

A safer approach is to treat variables + nested imports as path-scoped (run once per path) and only duplicate the resource object/namespace for keyword lookup/logging. For example: ensure the base resource is imported/stored under path once, and for aliases store resource.copy(name=resolved_alias) under a separate key without re-processing variables/imports.

Copilot uses AI. Check for mistakes.
Comment on lines +99 to +103
key = (path, import_.alias) if import_.alias else path
if overwrite or key not in self._kw_store.resources:
resource = IMPORTER.import_resource(path, self.languages)
if import_.alias:
resource = resource.copy(name=import_.alias)

Copilot AI Mar 25, 2026

Copy link

Choose a reason for hiding this comment

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

import_.alias is used verbatim when creating the aliased resource (resource.copy(name=import_.alias)). Unlike library aliases (which are resolved via variables.replace_scalar() in Importer.import_library()), resource aliases won’t get variable replacement, so WITH NAME ${ALIAS} will not work as expected. Consider resolving the alias through self.variables before using it for the cache key and before copying the resource.

Suggested change

Copilot uses AI. Check for mistakes.
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.

3 participants