Skip to content

Report errors if no files were found or if source-directories are empty #352

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

Merged
merged 3 commits into from
Feb 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions lib/elm-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const Debug = require('./debug');
const ErrorMessage = require('./error-message');
const FS = require('./fs-wrapper');
const Hash = require('./hash');
const {unique} = require('./utils');
const OsHelpers = require('./os-helpers');
const elmParser = require('./parse-elm');
const AppState = require('./state');
Expand Down Expand Up @@ -52,6 +53,9 @@ async function getProjectFiles(options, elmSyntaxVersion) {
const emptyDirectories = filesInDirectories.filter(
({files}) => files.length === 0
);
const elmFilesToRead = [
...new Set(filesInDirectories.flatMap((directory) => directory.files))
];
Comment on lines +56 to +58
Copy link
Contributor

Choose a reason for hiding this comment

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

Just noticed that is just unique() inlined. Maybe I'll de-inline it someday. Probably not. :)

Copy link
Owner Author

Choose a reason for hiding this comment

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

Ah right. Yeah I reached out for unique after writing this code. I'll make the change.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was rebasing and this made a conflict, so I fixed it in #344.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Just saw your message. I fixed it in #354, it's on master now 😅

Copy link
Contributor

@lishaduck lishaduck Feb 23, 2025

Choose a reason for hiding this comment

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

Already re-(re-?)rebased 🤣


// TODO(@jfmengels): Have a different message depending on how directories were chosen (CLI directories, application, package).
if (options.directoriesToAnalyze.length > 0 && emptyDirectories.length > 0) {
Expand All @@ -64,6 +68,12 @@ ${emptyDirectories.map(({directory}) => `- ${directory}`).join('\n')}
When I can’t find files in some of the directories, I’m assuming that you
misconfigured the CLI’s arguments.`
);
} else if (elmFilesToRead.length === 0) {
throw new ErrorMessage.CustomError(
'NO FILES FOUND',
`I could not find any files in this project. I looked in these folders:
${sourceDirectories.map((directory) => `- ${directory}`).join('\n')}`
);
}

Debug.log(`Parsing using stil4m/elm-syntax v${elmSyntaxVersion}`);
Expand All @@ -72,9 +82,6 @@ misconfigured the CLI’s arguments.`
Benchmark.start(options, 'parse/fetch parsed files');
const elmParserPath = options.elmParserPath(elmSyntaxVersion);
elmParser.prepareWorkers();
const elmFilesToRead = [
...new Set(filesInDirectories.flatMap((directory) => directory.files))
];
const elmFiles = await Promise.all(
elmFilesToRead.map(
async (filePath) =>
Expand Down Expand Up @@ -232,8 +239,10 @@ function getSourceDirectories(options, elmJson) {
if (options.directoriesToAnalyze.length > 0) {
return {
isFromCliArguments: true,
sourceDirectories: options.directoriesToAnalyze.map((directory) =>
path.resolve(process.cwd(), directory)
sourceDirectories: unique(
options.directoriesToAnalyze.map((directory) =>
path.resolve(process.cwd(), directory)
)
)
};
}
Expand Down Expand Up @@ -262,16 +271,23 @@ function standardSourceDirectories(options, elmJson) {
}

// If the project is an "application"
const sourceDirectories = elmJson['source-directories'] ?? [];
if (sourceDirectories.length === 0) {
throw new ErrorMessage.CustomError(
'EMPTY SOURCE-DIRECTORIES',
`The \`source-directories\` in your \`elm.json\` is empty. I need it to contain
at least 1 directory in order to find files to analyze. The Elm compiler will
need that as well anyway.`
);
}

return [
...elmJson['source-directories'].flatMap(
(/** @type {Path} */ directory) => [
path.join(projectToReview, directory),
path.resolve(projectToReview, directory, '../tests/')
]
),
return unique([
...sourceDirectories.flatMap((/** @type {Path} */ directory) => [
path.join(projectToReview, directory),
path.resolve(projectToReview, directory, '../tests/')
]),
path.join(projectToReview, 'tests')
];
]);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions test/project-empty-source-directories/elm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"type": "application",
"source-directories": [],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/core": "1.0.5",
"elm/html": "1.0.0"
},
"indirect": {
"elm/json": "1.1.3",
"elm/virtual-dom": "1.0.3"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}
13 changes: 13 additions & 0 deletions test/project-empty-source-directories/src/Main.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Main exposing (main)

import Html exposing (Html)


main : Html msg
main =
Html.text "main"


unused : Int
unused =
1
24 changes: 24 additions & 0 deletions test/project-empty/elm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"type": "application",
"source-directories": [
"src"
],
"elm-version": "0.19.1",
"dependencies": {
"direct": {
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0"
},
"indirect": {
"elm/json": "1.1.3",
"elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.3"
}
},
"test-dependencies": {
"direct": {},
"indirect": {}
}
}
Empty file.
16 changes: 16 additions & 0 deletions test/review.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,22 @@ test('Running on project with unknown file', async () => {
expect(output).toMatchFile(testName('run-with-unknown-target'));
});

test('Running on project with no files', async () => {
const output = await TestCli.runAndExpectError(
['--config=../project-with-errors/review'],
{project: 'project-empty'}
);
expect(output).toMatchFile(testName('run-with-empty-project'));
});

test('Running on project with no files', async () => {
const output = await TestCli.runAndExpectError(
['--config=../project-with-errors/review'],
{project: 'project-empty-source-directories'}
);
expect(output).toMatchFile(testName('run-with-empty-source-directories'));
});

test('Running on project with a directory ending in .elm (without arg)', async () => {
const output = await TestCli.run(
['--config', '../config-that-triggers-no-errors'],
Expand Down
6 changes: 6 additions & 0 deletions test/snapshots/review/run-with-empty-project.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- NO FILES FOUND --------------------------------------------------------------

I could not find any files in this project. I looked in these folders:
- <local-path>/test/project-empty/src
- <local-path>/test/project-empty/tests

6 changes: 6 additions & 0 deletions test/snapshots/review/run-with-empty-source-directories.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- EMPTY SOURCE-DIRECTORIES ----------------------------------------------------

The `source-directories` in your `elm.json` is empty. I need it to contain
at least 1 directory in order to find files to analyze. The Elm compiler will
need that as well anyway.