How should a missing project reference be programmatically removed? ('removeReference()' exits with an error)

2 次查看(过去 30 天)
MATLAB R2023a Update 6
Normally, removeReference() can be used to remove a project reference from the top-level project. When I try to use this function on a stale project reference, the function errors with the following error text (redacted local file paths):
>> removeReference(matlab.project.rootProject, stale_project_path)
Error using matlab.internal.project.japi.Project/removeReference
The file or folder "<stale_project_path value>" does not exist.
Error in matlab.project.Project/removeReference
I've also tried using the matlab.project.ProjectReference object directly:
>> broken_ref = matlab.project.rootProject().ProjectReferences(1)
ProjectReference with properties:
...
>> removeReference(matlab.project.rootProject, broken_ref)
Error using matlab.internal.project.japi.Project/removeReference
The file or folder "<broken_ref.File value>" does not exist.
Error in matlab.project.Project/removeReference
Removing the stale project with the GUI works fine, so it's strange that removeReference() refuses to remove the broken project reference.
As a weird workaround, if I create the folder for the stale project reference before trying to remove the reference, I can then call removeReference() without getting an error. I just need to follow up and delete the dummy project folder afterwards.
Example workaround snippet:
broken_ref = matlab.project.rootProject().ProjectReferences(1)
% Create a temporary empty directory at the missing project location
mkdir(broken_ref.StoredLocation)
% Remove broken project reference
removeReference(matlab.project.rootProject, broken_ref)
% Clean up the temporary directories
rmdir(broken_ref.StoredLocation)
tree_path = fileparts(broken_ref.StoredLocation)
% Walk up the directory tree,
% deleting the empty temporary directories as we go.
try
while strlength(tree_path) ~= 0
rmdir(tree_path)
tree_path = fileparts(tree_path)
end
catch
disp("Aborting the deletion of remaining path parts since a directory (""" + tree_path + """) was not empty.")
disp("Therefore, any empty directories that were created during this script should have been deleted while existing, non-empty directories should be untouched.")
end
disp("Successfully removed broken project reference: """ + broken_ref.StoredLocation + """")
% Workspace variable cleanup
clear tree_path
clear broken_ref
For now, the workaround snippet is good enough for me, but it's weird that removeReference() can't do what the respective GUI feature can do and for what seems to me a valid use-case.

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Search Path 的更多信息

产品


版本

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by