Skip to content

Commit

Permalink
Merge pull request #2099 from xodio/fix-2097-do-not-load-invalid-patc…
Browse files Browse the repository at this point in the history
…h-paths

Do not load project with invalid patch paths
  • Loading branch information
brusherru authored Feb 24, 2021
2 parents 5032a2f + 681f3f9 commit 1bb00e0
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 15 deletions.
26 changes: 12 additions & 14 deletions packages/xod-client-electron/src/app/workspaceActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,18 @@ export const onIDELaunch = R.curry(
}),
getFileToOpen()
)
)()
.catch(
catchInvalidWorkspace(err => {
const isHomeDir = oldPath !== newPath;
const forceCreate =
err.errorCode === FS_ERROR_CODES.WORKSPACE_DIR_NOT_EMPTY;
if (isHomeDir && !forceCreate) {
return onCreateWorkspace(send, pathSaver, newPath);
}
requestCreateWorkspace(send, newPath, forceCreate);
return [];
})
)
.catch(handleError(send));
)().catch(
catchInvalidWorkspace(err => {
const isHomeDir = oldPath !== newPath;
const forceCreate =
err.errorCode === FS_ERROR_CODES.WORKSPACE_DIR_NOT_EMPTY;
if (isHomeDir && !forceCreate) {
return onCreateWorkspace(send, pathSaver, newPath);
}
requestCreateWorkspace(send, newPath, forceCreate);
return [];
})
);
}
);

Expand Down
2 changes: 2 additions & 0 deletions packages/xod-client-electron/src/view/formatError.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { messages as xpMessages } from 'xod-project';
import { messages as xdMessages } from 'xod-deploy';
import { messages as xdbMessages } from 'xod-deploy-bin';
import { messages as xardMessages } from 'xod-arduino';
import { messages as xfMessages } from 'xod-fs';

import uploadMessages from '../upload/messages';

Expand All @@ -11,6 +12,7 @@ export const formatErrorMessage = composeErrorFormatters([
xdMessages,
xdbMessages,
xardMessages,
xfMessages,
uploadMessages,
]);

Expand Down
3 changes: 3 additions & 0 deletions packages/xod-fs/src/errorCodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ export const CANT_ENUMERATE_PROJECTS = 'CANT_ENUMERATE_PROJECTS';
export const CANT_SAVE_PROJECT = 'CANT_SAVE_PROJECT';
export const CANT_SAVE_LIBRARY = 'CANT_SAVE_LIBRARY';
export const INVALID_FILE_CONTENTS = 'INVALID_FILE_CONTENTS';

export const PROJECT_LOADED_WITH_INVALID_PATCH_PATHS =
'PROJECT_LOADED_WITH_INVALID_PATCH_PATHS';
1 change: 1 addition & 0 deletions packages/xod-fs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export {
isWorkspaceDir,
getPathToXodProject,
} from './find';
export { default as messages } from './messages';

export * from './constants';

Expand Down
33 changes: 32 additions & 1 deletion packages/xod-fs/src/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@ import {
addMissingOptionsToPatchFileContents,
addMissingOptionsToProjectFileContents,
} from './convertTypes';

// =============================================================================
//
// Validate loaded project
//
// xod-fs returns a `LoadResult` type, which contains a validated
// and probably patched with some fixes Project and a list of warnings
//
// =============================================================================

// :: Project -> Promise Project Error
const validateLoadedProject = project =>
R.compose(
R.ifElse(
corrupted => corrupted.length,
patchPaths =>
Promise.reject(
XF.createError(ERROR_CODES.PROJECT_LOADED_WITH_INVALID_PATCH_PATHS, {
patchPaths,
})
),
() => Promise.resolve(project)
),
R.filter(
R.both(R.complement(XP.isPathLocal), R.complement(XP.isPathLibrary))
),
R.map(XP.getPatchPath),
XP.listGenuinePatches
)(project);

// =============================================================================
//
// Reading of files
Expand Down Expand Up @@ -220,9 +250,10 @@ export const loadProjectFromXodball = R.curry((workspaceDirs, xodballPath) =>
* If other extension is passed into this function it will return
* rejected Promise with Error. Otherwise, Promise Project.
*/
// :: [Path] -> Path -> Promise Project Error
// :: [Path] -> Path -> Promise Promise Error
export const loadProject = R.uncurryN(2, workspaceDirs =>
R.composeP(
validateLoadedProject,
XP.migrateBoundValuesToBoundLiterals,
R.ifElse(
isExtname('.xodball'),
Expand Down
21 changes: 21 additions & 0 deletions packages/xod-fs/src/messages.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as R from 'ramda';
import { PROJECT_LOADED_WITH_INVALID_PATCH_PATHS } from './errorCodes';

const formatPatchPaths = R.compose(
R.join(', '),
R.when(
patchPaths => patchPaths.length > 3,
R.compose(R.append('…'), R.take(3))
)
);

export default {
[PROJECT_LOADED_WITH_INVALID_PATCH_PATHS]: ({ patchPaths }) => ({
title: 'Cannot load some patches',
note: `Some patches have an invalid patch paths: ${formatPatchPaths(
patchPaths
)}`,
solution:
'Open the "__lib__" directory in your workspace and ensure that listed directory names are valid. Only lowercase latin alphabet, numbers, and hypens are allowed.',
}),
};

0 comments on commit 1bb00e0

Please sign in to comment.