-
Notifications
You must be signed in to change notification settings - Fork 876
fix: locate Terraform entrypoint file #16753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,7 +90,7 @@ export const TemplateVersionEditorPage: FC = () => { | |
// File navigation | ||
// It can be undefined when a selected file is deleted | ||
const activePath: string | undefined = | ||
searchParams.get("path") ?? findInitialFile(fileTree ?? {}); | ||
searchParams.get("path") ?? findEntrypointFile(fileTree ?? {}); | ||
const onActivePathChange = (path: string | undefined) => { | ||
if (path) { | ||
searchParams.set("path", path); | ||
|
@@ -357,10 +357,33 @@ const publishVersion = async (options: { | |
return Promise.all(publishActions); | ||
}; | ||
|
||
const findInitialFile = (fileTree: FileTree): string | undefined => { | ||
const defaultMainTerraformFile = "main.tf"; | ||
|
||
// findEntrypointFile function locates the entrypoint file to open in the Editor. | ||
// It browses the filetree following these steps: | ||
// 1. If "main.tf" exists in root, return it. | ||
// 2. Traverse through sub-directories. | ||
// 3. If "main.tf" exists in a sub-directory, skip further browsing, and return the path. | ||
// 4. If "main.tf" was not found, return the last reviewed "".tf" file. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the sorting in place now, I feel like it would've helped to clarify what "last reviewed" means, especially to help make it clear that the tests are actually deterministic |
||
export const findEntrypointFile = (fileTree: FileTree): string | undefined => { | ||
let initialFile: string | undefined; | ||
|
||
traverse(fileTree, (content, filename, path) => { | ||
if (Object.keys(fileTree).find((key) => key === defaultMainTerraformFile)) { | ||
return defaultMainTerraformFile; | ||
} | ||
|
||
let skip = false; | ||
traverse(fileTree, (_, filename, path) => { | ||
if (skip) { | ||
return; | ||
} | ||
|
||
if (filename === defaultMainTerraformFile) { | ||
initialFile = path; | ||
skip = true; | ||
return; | ||
} | ||
Comment on lines
+381
to
+385
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the order in which There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me expand There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice 👍 |
||
|
||
if (filename.endsWith(".tf")) { | ||
initialFile = path; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know what happens if
searchParams.get
returns an empty string or a string that isn't a valid filename?That would short-circuit the nullish expression and prevent
findEntrypointFile
from ever being called, and as far as I can tell,activePath
gets fed directly into the editor without any additional data massagingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice catch! let me look into this 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#16757