Skip to content

Conversation

teresaromero
Copy link
Contributor

@teresaromero teresaromero commented Sep 25, 2025

Resolves #2797

This pull request refactors how repository root and links definitions files are discovered and used throughout the codebase, improving consistency, error handling, and test coverage. The main changes include passing the repository root explicitly to functions that need it, updating the logic for finding and reading the links definitions file, and enhancing license file handling during package builds. Comprehensive unit tests have been added for these new behaviors.

Repository root and links file handling:

  • All commands (build, lint, and links-related commands in cmd/build.go, cmd/lint.go, cmd/links.go) now explicitly locate and pass the repository root using files.FindRepositoryRoot(), and use this to find the links definitions file via docs.LinksDefinitionsFilePath(repoRoot). This improves reliability and consistency in locating project resources. [1] [2] [3] [4] [5] [6]

License file handling during build:

  • The build logic in internal/builder/packages.go now takes the repository root as a parameter and uses it to locate and copy the license file into the package, supporting overrides via environment variable and handling missing files gracefully. The helper functions have been refactored for clarity and improved error handling. [1] [2] [3] [4] [5]

Links map logic and API changes:

  • The links map implementation in internal/docs/links_map.go has been refactored to use pointer receivers, provide clearer error handling, and accept the links file path explicitly. The logic for finding the links definitions file now checks environment variables first and falls back to the default file in the repository root, returning an empty string if not found. [1] [2] [3]

Unit tests for new behaviors:

  • New and updated tests in internal/builder/packages_test.go and internal/docs/links_map_test.go cover repository license file discovery, license copying logic, and links file resolution under various scenarios (env var, missing file, default file, YAML parsing). [1] [2]

Imports and cleanup:

  • Imports have been updated to support the new logic, and unused imports have been removed for clarity. [1] [2]

@teresaromero teresaromero marked this pull request as ready for review September 30, 2025 13:01
@teresaromero
Copy link
Contributor Author

@mrodm i've updated the code with your feedback. i usually mark resolved the comments that have been addressed, please re-open if you find anything pending.

thanks

Copy link
Member

@jsoriano jsoriano left a comment

Choose a reason for hiding this comment

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

It looks great, it seems to solve links resolution and does a good refactor to allow injection of the path to the root repository.

I did some tests and found some issues, commented in the files, though maybe some of them can be solved in follow ups.

Many comments are nitpicking, no need to do anything about them if you prefer current code.

Copy link
Member

Choose a reason for hiding this comment

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

I have tried to link a fields file that includes fields with external: ecs and elastic-package fails to resolve these fields when building the package, with errors like:

Error: building package failed: invalid content found in built zip package: found 7 validation errors:
   1. file ".../github.com/elastic/integrations/build/packages/system-2.6.1.zip/data_stream/cpu/fields/agent.yml" is invalid: field cloud.account.id with external key defined ("ecs") but no _dev/build/build.yml found

I guess it is trying to read the build.yml file from the target directory instead of from the source directory. It only happens with linked files.

Not sure if the issue is introduced with the current PR, so we can handle this in a follow up, as you prefer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

could you walk me through this test? i am not sure i understood correctly the operation here.

Create a .link file for ecs.yml of one package ?

Copy link
Member

Choose a reason for hiding this comment

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

This can be reproduced with the following commands, from the integrations repository:

mkdir packages/system/_dev/shared
mv packages/system/data_stream/cpu/fields/agent.yml packages/system/_dev/shared/agent.yml
echo ../../../_dev/shared/agent.yml > packages/system/data_stream/cpu/fields/agent.yml.link
elastic-package links update
elastic-package build -C packages/system

@elasticmachine
Copy link
Collaborator

elasticmachine commented Oct 16, 2025

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.

Refactor links resolver so it can write to files out of the repository

4 participants