Skip to content

Commit 3eba702

Browse files
tmdsjozkee
andauthored
Console.Unix: revert SetWindowSize implementation. (#100272)
* Console.Unix: revert SetWindowSize implementation. SetWindowSize was implemented using using TIOCSWINSZ. TIOCSWINSZ is meant to inform the kernel of the terminal size. The window that shows the terminal doesn't change to match that size. * Delete HAVE_TIOCSWINSZ. * Update compat supressions. * Code formatting. * Apply suggestions from code review --------- Co-authored-by: David Cantú <[email protected]>
1 parent 0fbc04b commit 3eba702

File tree

14 files changed

+87
-148
lines changed

14 files changed

+87
-148
lines changed

src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetWindowWidth.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,5 @@ internal struct WinSize
2020

2121
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_GetWindowSize", SetLastError = true)]
2222
internal static partial int GetWindowSize(SafeFileHandle terminalHandle, out WinSize winSize);
23-
24-
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_SetWindowSize", SetLastError = true)]
25-
internal static partial int SetWindowSize(in WinSize winSize);
2623
}
2724
}

src/libraries/System.Console/ref/System.Console.cs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,10 @@ public static partial class Console
6969
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
7070
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
7171
public static bool TreatControlCAsInput { get { throw null; } set { } }
72-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")]
73-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
74-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
75-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
76-
public static int WindowHeight { get { throw null; } set { } }
72+
public static int WindowHeight { [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android"), System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser"), System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios"), System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] get { throw null; } [System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")] set { } }
7773
public static int WindowLeft { get { throw null; } [System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")] set { } }
7874
public static int WindowTop { get { throw null; } [System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")] set { } }
79-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")]
80-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
81-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
82-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
83-
public static int WindowWidth { get { throw null; } set { } }
75+
public static int WindowWidth { [System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android"), System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser"), System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios"), System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")] get { throw null; } [System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")] set { } }
8476
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")]
8577
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
8678
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
@@ -155,10 +147,7 @@ public static void SetIn(System.IO.TextReader newIn) { }
155147
public static void SetOut(System.IO.TextWriter newOut) { }
156148
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
157149
public static void SetWindowPosition(int left, int top) { }
158-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("android")]
159-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("browser")]
160-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("ios")]
161-
[System.Runtime.Versioning.UnsupportedOSPlatformAttribute("tvos")]
150+
[System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
162151
public static void SetWindowSize(int width, int height) { }
163152
public static void Write(bool value) { }
164153
public static void Write(char value) { }

src/libraries/System.Console/src/System/Console.cs

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -392,42 +392,27 @@ public static int WindowTop
392392
set { ConsolePal.WindowTop = value; }
393393
}
394394

395-
[UnsupportedOSPlatform("android")]
396-
[UnsupportedOSPlatform("browser")]
397-
[UnsupportedOSPlatform("ios")]
398-
[UnsupportedOSPlatform("tvos")]
399395
public static int WindowWidth
400396
{
397+
[UnsupportedOSPlatform("android")]
398+
[UnsupportedOSPlatform("browser")]
399+
[UnsupportedOSPlatform("ios")]
400+
[UnsupportedOSPlatform("tvos")]
401401
get { return ConsolePal.WindowWidth; }
402-
set
403-
{
404-
if (Console.IsOutputRedirected)
405-
{
406-
throw new IOException(SR.InvalidOperation_SetWindowSize);
407-
}
408-
409-
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value, nameof(WindowWidth));
410-
411-
ConsolePal.WindowWidth = value;
412-
}
402+
[SupportedOSPlatform("windows")]
403+
set { ConsolePal.WindowWidth = value; }
413404
}
414405

415-
[UnsupportedOSPlatform("android")]
416-
[UnsupportedOSPlatform("browser")]
417-
[UnsupportedOSPlatform("ios")]
418-
[UnsupportedOSPlatform("tvos")]
419406
public static int WindowHeight
420407
{
408+
[UnsupportedOSPlatform("android")]
409+
[UnsupportedOSPlatform("browser")]
410+
[UnsupportedOSPlatform("ios")]
411+
[UnsupportedOSPlatform("tvos")]
421412
get { return ConsolePal.WindowHeight; }
413+
[SupportedOSPlatform("windows")]
422414
set
423415
{
424-
if (Console.IsOutputRedirected)
425-
{
426-
throw new IOException(SR.InvalidOperation_SetWindowSize);
427-
}
428-
429-
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(value, nameof(WindowHeight));
430-
431416
ConsolePal.WindowHeight = value;
432417
}
433418
}
@@ -438,20 +423,9 @@ public static void SetWindowPosition(int left, int top)
438423
ConsolePal.SetWindowPosition(left, top);
439424
}
440425

441-
[UnsupportedOSPlatform("android")]
442-
[UnsupportedOSPlatform("browser")]
443-
[UnsupportedOSPlatform("ios")]
444-
[UnsupportedOSPlatform("tvos")]
426+
[SupportedOSPlatform("windows")]
445427
public static void SetWindowSize(int width, int height)
446428
{
447-
if (Console.IsOutputRedirected)
448-
{
449-
throw new IOException(SR.InvalidOperation_SetWindowSize);
450-
}
451-
452-
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(width, nameof(width));
453-
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(height, nameof(height));
454-
455429
ConsolePal.SetWindowSize(width, height);
456430
}
457431

src/libraries/System.Console/src/System/ConsolePal.Unix.cs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -378,24 +378,11 @@ private static void GetWindowSize(out int width, out int height)
378378

379379
public static void SetWindowSize(int width, int height)
380380
{
381-
lock (Console.Out)
382-
{
383-
Interop.Sys.WinSize winsize = default;
384-
winsize.Row = (ushort)height;
385-
winsize.Col = (ushort)width;
386-
if (Interop.Sys.SetWindowSize(in winsize) == 0)
387-
{
388-
s_windowWidth = winsize.Col;
389-
s_windowHeight = winsize.Row;
390-
}
391-
else
392-
{
393-
Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
394-
throw errorInfo.Error == Interop.Error.ENOTSUP ?
395-
new PlatformNotSupportedException() :
396-
Interop.GetIOException(errorInfo);
397-
}
398-
}
381+
// note: We can't implement SetWindowSize using TIOCSWINSZ.
382+
// TIOCSWINSZ is meant to inform the kernel of the terminal size.
383+
// The window that shows the terminal doesn't change to match that size.
384+
385+
throw new PlatformNotSupportedException();
399386
}
400387

401388
public static bool CursorVisible

src/libraries/System.Console/src/System/ConsolePal.Windows.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,14 @@ public static unsafe void SetWindowPosition(int left, int top)
10121012

10131013
public static unsafe void SetWindowSize(int width, int height)
10141014
{
1015+
if (Console.IsOutputRedirected)
1016+
{
1017+
throw new IOException(SR.InvalidOperation_SetWindowSize);
1018+
}
1019+
1020+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(width, nameof(width));
1021+
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(height, nameof(height));
1022+
10151023
// Get the position of the current console window
10161024
Interop.Kernel32.CONSOLE_SCREEN_BUFFER_INFO csbi = GetBufferInfo();
10171025

src/libraries/System.Console/tests/ManualTests/ManualTests.cs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
using System.Diagnostics;
66
using System.Threading.Tasks;
77
using System.IO;
8-
using System.Runtime.InteropServices;
98
using System.Text;
10-
using System.Threading;
119
using Xunit;
1210

1311
namespace System
@@ -334,37 +332,6 @@ public static void CursorLeftFromLastColumn()
334332
AssertUserExpectedResults("single line with '1' at the start and '2' at the end.");
335333
}
336334

337-
[ConditionalFact(nameof(ManualTestsEnabled))]
338-
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS, "Not supported on Browser, iOS, MacCatalyst, or tvOS.")]
339-
public static void ResizeTest()
340-
{
341-
bool wasResized = false;
342-
343-
using (ManualResetEvent manualResetEvent = new(false))
344-
using (PosixSignalRegistration.Create(PosixSignal.SIGWINCH,
345-
ctx =>
346-
{
347-
wasResized = true;
348-
Assert.Equal(PosixSignal.SIGWINCH, ctx.Signal);
349-
manualResetEvent.Set();
350-
}))
351-
{
352-
int widthBefore = Console.WindowWidth;
353-
int heightBefore = Console.WindowHeight;
354-
355-
Assert.False(wasResized);
356-
357-
Console.SetWindowSize(widthBefore / 2, heightBefore / 2);
358-
359-
Assert.True(manualResetEvent.WaitOne(TimeSpan.FromMilliseconds(50)));
360-
Assert.True(wasResized);
361-
Assert.Equal(widthBefore / 2, Console.WindowWidth );
362-
Assert.Equal(heightBefore / 2, Console.WindowHeight );
363-
364-
Console.SetWindowSize(widthBefore, heightBefore);
365-
}
366-
}
367-
368335
private static void AssertUserExpectedResults(string expected)
369336
{
370337
Console.Write($"Did you see {expected}? [y/n] ");

src/libraries/System.Console/tests/WindowAndCursorProps.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public static void SetBufferSize_Unix_ThrowsPlatformNotSupportedException()
4646
}
4747

4848
[Theory]
49-
[PlatformSpecific((TestPlatforms.Windows) | (TestPlatforms.AnyUnix & ~TestPlatforms.Browser & ~TestPlatforms.iOS & ~TestPlatforms.MacCatalyst & ~TestPlatforms.tvOS))]
49+
[PlatformSpecific(TestPlatforms.Windows)]
5050
[InlineData(0)]
5151
[InlineData(-1)]
5252
public static void WindowWidth_SetInvalid_ThrowsArgumentOutOfRangeException(int value)
@@ -71,14 +71,14 @@ public static void WindowWidth_GetUnix_Success()
7171
}
7272

7373
[Fact]
74-
[PlatformSpecific(TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS)] // Expected behavior specific to Unix
74+
[PlatformSpecific(TestPlatforms.AnyUnix)]
7575
public static void WindowWidth_SetUnix_ThrowsPlatformNotSupportedException()
7676
{
7777
Assert.Throws<PlatformNotSupportedException>(() => Console.WindowWidth = 100);
7878
}
7979

8080
[Theory]
81-
[PlatformSpecific((TestPlatforms.Windows) | (TestPlatforms.AnyUnix & ~TestPlatforms.Browser & ~TestPlatforms.iOS & ~TestPlatforms.MacCatalyst & ~TestPlatforms.tvOS))]
81+
[PlatformSpecific(TestPlatforms.Windows)]
8282
[InlineData(0)]
8383
[InlineData(-1)]
8484
public static void WindowHeight_SetInvalid_ThrowsArgumentOutOfRangeException(int value)
@@ -111,7 +111,7 @@ public static void LargestWindowWidth_UnixGet_ReturnsExpected()
111111
}
112112

113113
[Fact]
114-
[PlatformSpecific(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS)] // Expected behavior specific to Unix
114+
[PlatformSpecific(TestPlatforms.AnyUnix)]
115115
public static void WindowHeight_SetUnix_ThrowsPlatformNotSupportedException()
116116
{
117117
Assert.Throws<PlatformNotSupportedException>(() => Console.WindowHeight = 100);
@@ -579,7 +579,7 @@ public void SetWindowSize_GetWindowSize_ReturnsExpected()
579579
}
580580

581581
[Fact]
582-
[PlatformSpecific(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.MacCatalyst | TestPlatforms.tvOS)]
582+
[PlatformSpecific(TestPlatforms.AnyUnix)]
583583
public void SetWindowSize_Unix_ThrowsPlatformNotSupportedException()
584584
{
585585
Assert.Throws<PlatformNotSupportedException>(() => Console.SetWindowSize(50, 50));

src/libraries/apicompat/ApiCompatBaseline.NetCoreAppLatestStable.xml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,24 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<!-- https://learn.microsoft.com/dotnet/fundamentals/package-validation/diagnostic-ids -->
33
<Suppressions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
4+
<Suppression>
5+
<DiagnosticId>CP0014</DiagnosticId>
6+
<Target>M:System.Console.SetWindowSize(System.Int32,System.Int32):[T:System.Runtime.Versioning.UnsupportedOSPlatformAttribute]</Target>
7+
<Left>net8.0/mscorlib.dll</Left>
8+
<Right>net9.0/mscorlib.dll</Right>
9+
</Suppression>
10+
<Suppression>
11+
<DiagnosticId>CP0014</DiagnosticId>
12+
<Target>P:System.Console.WindowHeight:[T:System.Runtime.Versioning.UnsupportedOSPlatformAttribute]</Target>
13+
<Left>net8.0/mscorlib.dll</Left>
14+
<Right>net9.0/mscorlib.dll</Right>
15+
</Suppression>
16+
<Suppression>
17+
<DiagnosticId>CP0014</DiagnosticId>
18+
<Target>P:System.Console.WindowWidth:[T:System.Runtime.Versioning.UnsupportedOSPlatformAttribute]</Target>
19+
<Left>net8.0/mscorlib.dll</Left>
20+
<Right>net9.0/mscorlib.dll</Right>
21+
</Suppression>
422
<Suppression>
523
<DiagnosticId>CP0014</DiagnosticId>
624
<Target>M:System.ComponentModel.DesignerAttribute.#ctor(System.String,System.String)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute]</Target>
@@ -91,6 +109,12 @@
91109
<Left>net8.0/netstandard.dll</Left>
92110
<Right>net9.0/netstandard.dll</Right>
93111
</Suppression>
112+
<Suppression>
113+
<DiagnosticId>CP0014</DiagnosticId>
114+
<Target>M:System.Console.SetWindowSize(System.Int32,System.Int32):[T:System.Runtime.Versioning.UnsupportedOSPlatformAttribute]</Target>
115+
<Left>net8.0/netstandard.dll</Left>
116+
<Right>net9.0/netstandard.dll</Right>
117+
</Suppression>
94118
<Suppression>
95119
<DiagnosticId>CP0014</DiagnosticId>
96120
<Target>P:System.ComponentModel.DesignerAttribute.DesignerBaseTypeName:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute]</Target>
@@ -121,6 +145,18 @@
121145
<Left>net8.0/netstandard.dll</Left>
122146
<Right>net9.0/netstandard.dll</Right>
123147
</Suppression>
148+
<Suppression>
149+
<DiagnosticId>CP0014</DiagnosticId>
150+
<Target>P:System.Console.WindowHeight:[T:System.Runtime.Versioning.UnsupportedOSPlatformAttribute]</Target>
151+
<Left>net8.0/netstandard.dll</Left>
152+
<Right>net9.0/netstandard.dll</Right>
153+
</Suppression>
154+
<Suppression>
155+
<DiagnosticId>CP0014</DiagnosticId>
156+
<Target>P:System.Console.WindowWidth:[T:System.Runtime.Versioning.UnsupportedOSPlatformAttribute]</Target>
157+
<Left>net8.0/netstandard.dll</Left>
158+
<Right>net9.0/netstandard.dll</Right>
159+
</Suppression>
124160
<Suppression>
125161
<DiagnosticId>CP0014</DiagnosticId>
126162
<Target>M:System.ComponentModel.DesignerAttribute.#ctor(System.String,System.String)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute]</Target>
@@ -349,6 +385,24 @@
349385
<Left>net8.0/System.ComponentModel.TypeConverter.dll</Left>
350386
<Right>net9.0/System.ComponentModel.TypeConverter.dll</Right>
351387
</Suppression>
388+
<Suppression>
389+
<DiagnosticId>CP0014</DiagnosticId>
390+
<Target>M:System.Console.SetWindowSize(System.Int32,System.Int32):[T:System.Runtime.Versioning.UnsupportedOSPlatformAttribute]</Target>
391+
<Left>net8.0/System.Console.dll</Left>
392+
<Right>net9.0/System.Console.dll</Right>
393+
</Suppression>
394+
<Suppression>
395+
<DiagnosticId>CP0014</DiagnosticId>
396+
<Target>P:System.Console.WindowHeight:[T:System.Runtime.Versioning.UnsupportedOSPlatformAttribute]</Target>
397+
<Left>net8.0/System.Console.dll</Left>
398+
<Right>net9.0/System.Console.dll</Right>
399+
</Suppression>
400+
<Suppression>
401+
<DiagnosticId>CP0014</DiagnosticId>
402+
<Target>P:System.Console.WindowWidth:[T:System.Runtime.Versioning.UnsupportedOSPlatformAttribute]</Target>
403+
<Left>net8.0/System.Console.dll</Left>
404+
<Right>net9.0/System.Console.dll</Right>
405+
</Suppression>
352406
<Suppression>
353407
<DiagnosticId>CP0014</DiagnosticId>
354408
<Target>M:System.ComponentModel.DesignerAttribute.#ctor(System.String,System.String)$0:[T:System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute]</Target>

src/native/libs/Common/pal_config.h.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
#cmakedefine01 HAVE_IOCTL
4646
#cmakedefine01 HAVE_IOCTL_WITH_INT_REQUEST
4747
#cmakedefine01 HAVE_TIOCGWINSZ
48-
#cmakedefine01 HAVE_TIOCSWINSZ
4948
#cmakedefine01 HAVE_SCHED_GETAFFINITY
5049
#cmakedefine01 HAVE_SCHED_SETAFFINITY
5150
#cmakedefine01 HAVE_SCHED_GETCPU

src/native/libs/System.Native/entrypoints.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ static const Entry s_sysNative[] =
3535
{
3636
DllImportEntry(SystemNative_FStat)
3737
DllImportEntry(SystemNative_GetWindowSize)
38-
DllImportEntry(SystemNative_SetWindowSize)
3938
DllImportEntry(SystemNative_IsATty)
4039
DllImportEntry(SystemNative_InitializeTerminalAndSignalHandling)
4140
DllImportEntry(SystemNative_SetKeypadXmit)

src/native/libs/System.Native/pal_console.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,6 @@ int32_t SystemNative_GetWindowSize(intptr_t fd, WinSize* windowSize)
4040
#endif
4141
}
4242

43-
int32_t SystemNative_SetWindowSize(WinSize* windowSize)
44-
{
45-
assert(windowSize != NULL);
46-
47-
#if HAVE_IOCTL_WITH_INT_REQUEST && HAVE_TIOCSWINSZ
48-
return ioctl(STDOUT_FILENO, (int)TIOCSWINSZ, windowSize);
49-
#elif HAVE_IOCTL && HAVE_TIOCSWINSZ
50-
return ioctl(STDOUT_FILENO, TIOCSWINSZ, windowSize);
51-
#else
52-
// Not supported on e.g. Android. Also, prevent a compiler error because windowSize is unused
53-
(void)windowSize;
54-
errno = ENOTSUP;
55-
return -1;
56-
#endif
57-
}
58-
5943
int32_t SystemNative_IsATty(intptr_t fd)
6044
{
6145
return isatty(ToFileDescriptor(fd));

src/native/libs/System.Native/pal_console.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,6 @@ typedef struct
4848
*/
4949
PALEXPORT int32_t SystemNative_GetWindowSize(intptr_t fd, WinSize* windowsSize);
5050

51-
/**
52-
* Sets the windows size of the terminal
53-
*
54-
* Returns 0 on success; otherwise, returns -1 and sets errno.
55-
*/
56-
PALEXPORT int32_t SystemNative_SetWindowSize(WinSize* windowsSize);
57-
5851
/**
5952
* Gets whether the specified file descriptor is for a terminal.
6053
*

0 commit comments

Comments
 (0)