diff --git a/CHANGELOG.md b/CHANGELOG.md index 537232e..0cc8f61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ All notable changes to this project will be documented in this file. ### Added - support preparing renames for here-documents ([#129](https://github.com/rcjsuen/dockerfile-language-service/issues/129)) +### Fixed +- stop resolving definitions to build stages after the selected line ([#130](https://github.com/rcjsuen/dockerfile-language-service/issues/130)) + ## [0.14.0] - 2024-06-18 ### Added - support computing highlight ranges for heredocs ([#121](https://github.com/rcjsuen/dockerfile-language-service/issues/121)) diff --git a/src/dockerDefinition.ts b/src/dockerDefinition.ts index eb6f5c0..00c21ea 100644 --- a/src/dockerDefinition.ts +++ b/src/dockerDefinition.ts @@ -42,7 +42,7 @@ export class DockerDefinition { if (Util.isInsideRange(position, range)) { const stageName = instruction.getImageName(); for (const from of dockerfile.getFROMs()) { - if (stageName === from.getBuildStage()) { + if (stageName === from.getBuildStage() && from.getRange().start.line < range.start.line) { return from.getBuildStageRange(); } } diff --git a/test/dockerDefinition.test.ts b/test/dockerDefinition.test.ts index 39cc71e..aa35789 100644 --- a/test/dockerDefinition.test.ts +++ b/test/dockerDefinition.test.ts @@ -120,6 +120,26 @@ describe("Dockerfile Document Definition tests", function () { let location = computeDefinition(document, Position.create(1, 10)); assert.strictEqual(location, null); }); + + describe("stage name shadowing an image", () => { + it("referencing a later stage", () => { + const document = "FROM alpine\nFROM scratch AS alpine"; + let location = computeDefinition(document, Position.create(0, 8)); + assert.strictEqual(location, null); + + location = computeDefinition(document, Position.create(1, 19)); + assertLocation(location, 1, 16, 1, 22); + }); + + it("referencing itself", () => { + const document = "FROM alpine AS alpine"; + let location = computeDefinition(document, Position.create(0, 8)); + assert.strictEqual(location, null); + + location = computeDefinition(document, Position.create(0, 18)); + assertLocation(location, 0, 15, 0, 21); + }); + }); }); });