feature #5609 Support WITH NAME alias for Resource file imports#5632
feature #5609 Support WITH NAME alias for Resource file imports#5632swapneilbasutkar wants to merge 1 commit into
WITH NAME alias for Resource file imports#5632Conversation
WITH NAME alias for Resource file importsWITH NAME alias for Resource file imports
themavik
left a comment
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Resourceimports. - 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
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -48,11 +47,11 @@ class Settings(ABC): | |||
| "Setup", | |||
| "Teardown", | |||
| "Template", | |||
| "Resource", | |||
| "Variables", | |||
| ) | |||
| name_arguments_and_with_name = ( | |||
| "Library", | |||
| "Resource", | |||
| ) # fmt: skip | |||
There was a problem hiding this comment.
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()).
| 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) |
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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.

This PR introduces the ability to import Resource files with an alias using the
WITH NAMEsyntax, 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:
WITH NAMEalias when reading a [Resource].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