Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to NOT do to prevent Delphi from mangling the uses list and {$*.RES} in a .DPR

Every couple of weeks, I bump into this: when doing an IDE operation on the uses units in a Delphi project, it mangles the .dpr file.

What happens is that it rebuilds the uses list, but gets the position wrong.

I'm wondering what usage pattern to avoid so I won't get into this error again.

I've had this error occur in many Delphi versions. I know it exists in at least Delphi XE2 (it happened there again today), XE, 2007, 2006 and 7.

The mangled fragment usually is structured like this:

ususes
  Forms,
  ..
  LastUnitInUses in 'LastUnitInUses.pas';

R *.RES}

and should be corrected by removing one us, and adding a {$:

uses
  Forms,
  ..
  LastUnitInUses in 'LastUnitInUses.pas';

{R *.RES}

Example file that went wrong:

program SysUtilsFormatTests;
{

  Delphi DUnit Test Project
  -------------------------
  This project contains the DUnit test framework and the GUI/Console test runners.
  Add "CONSOLE_TESTRUNNER" to the conditional defines entry in the project options
  to use the console test runner.  Otherwise the GUI test runner will be used by
  default.

}

{$IFDEF CONSOLE_TESTRUNNER}
{$APPTYPE CONSOLE}
{$ENDIF}

ususes
  Forms,
  TestFramework,
  GUITestRunner,
  TextTestRunner,
  SysUtilsFormatUnit in 'SysUtilsFormatUnit.pas';

R *.RES}

begin
  Application.Initialize;
  if IsConsole then
    with TextTestRunner.RunRegisteredTests do
      Free
  else
    GUITestRunner.RunRegisteredTests;
end.

Example of corrected .dpr file:

program SysUtilsFormatTests;
{

  Delphi DUnit Test Project
  -------------------------
  This project contains the DUnit test framework and the GUI/Console test runners.
  Add "CONSOLE_TESTRUNNER" to the conditional defines entry in the project options
  to use the console test runner.  Otherwise the GUI test runner will be used by
  default.

}

{$IFDEF CONSOLE_TESTRUNNER}
{$APPTYPE CONSOLE}
{$ENDIF}

uses
  Forms,
  TestFramework,
  GUITestRunner,
  TextTestRunner,
  SysUtilsFormatUnit in 'SysUtilsFormatUnit.pas';

{$R *.RES}

begin
  Application.Initialize;
  if IsConsole then
    with TextTestRunner.RunRegisteredTests do
      Free
  else
    GUITestRunner.RunRegisteredTests;
end.
like image 383
Jeroen Wiert Pluimers Avatar asked May 27 '13 14:05

Jeroen Wiert Pluimers


1 Answers

The only thing that I know that works is for you to let the IDE manage the .dpr file.

  • Don't add comments.
  • Don't use conditionals like $IFDEF.
  • Don't modify the code in the .dpr file.

if you do any of these things, expect the IDE to bite back.

Personally I do all of these and fight back at commit time. I use my VCS to defend against bogus IDE changes. It's not ideal, but it's the best option.

like image 173
David Heffernan Avatar answered Oct 13 '22 12:10

David Heffernan